give up and do part 2 via a for loop

This commit is contained in:
2025-12-02 23:24:13 -05:00
parent 23d6769923
commit 980a514022
2 changed files with 39 additions and 23 deletions

View File

@@ -18,9 +18,9 @@ public class Day00 {
sb.append("\n");
}
String input = sb.toString();
System.out.println(input);
IO.println(input);
} catch (Exception e) {
System.out.println(e.getMessage());
IO.println(e.getMessage());
}
}
@@ -33,9 +33,9 @@ public class Day00 {
sb.append("\n");
}
String input = sb.toString();
System.out.println(input);
IO.println(input);
} catch (Exception e) {
System.out.println(e.getMessage());
IO.println(e.getMessage());
}
}
}

View File

@@ -6,7 +6,7 @@ import java.util.Scanner;
public class Day02 {
public static void main(String[] args) {
part1();
// part2();
part2();
}
private static void part1() {
@@ -26,24 +26,31 @@ public class Day02 {
start = permute(start);
}
}
System.out.println(sum);
IO.println(sum);
} catch (Exception e) {
System.out.println(e.getMessage());
IO.println(e.getMessage());
}
}
// fine, we'll just iterate the whole range
// can probably do this by bitshifting instead of long<->string conversions
private static void part2() {
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/02/input"))) {
StringBuilder sb = new StringBuilder();
while (scanner.hasNextLine()) {
sb.append(scanner.nextLine());
sb.append("\n");
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/02/input")).useDelimiter(",")) {
long sum = 0L;
while (scanner.hasNext()) {
String input = scanner.next().trim();
String[] range = input.split("-");
long start = Long.parseLong(range[0]);
long end = Long.parseLong(range[1]);
for (long i = start; i <= end; i++) {
if (hasPattern(String.valueOf(i))) {
sum += i;
}
}
}
String input = sb.toString();
System.out.println(input);
IO.println(sum);
} catch (Exception e) {
System.out.println(e.getMessage());
IO.println(e.getMessage());
}
}
@@ -52,23 +59,32 @@ public class Day02 {
// odd length, can't have a repeated pair
return false;
}
return CharSequence.compare(
id.subSequence(0, id.length() / 2),
id.subSequence(id.length() / 2, id.length())) == 0;
return id.substring(0, id.length() / 2) == id.substring(id.length() / 2);
}
private static String permute(String str) {
if (str.length() % 2 != 0) {
// in this case, we need to get to a even-length string
return String.valueOf((int) Math.pow(10, str.length() / 2))
+ String.valueOf((int) Math.pow(10, str.length() / 2));
return String.valueOf((long) Math.pow(10, str.length() / 2))
+ String.valueOf((long) Math.pow(10, str.length() / 2));
}
long head = Long.parseLong(str.subSequence(0, str.length() / 2).toString());
long tail = Long.parseLong(str.subSequence(str.length() / 2, str.length()).toString());
long head = Long.parseLong(str.substring(0, str.length() / 2));
long tail = Long.parseLong(str.substring(str.length() / 2, str.length()));
if (tail < head) {
return String.valueOf(head) + String.valueOf(head);
}
head++;
return String.valueOf(head) + String.valueOf(head);
}
// don't need regexes, at least
private static boolean hasPattern(String str) {
for (int patternLength = 1; patternLength <= str.length() / 2; patternLength++) {
String candidate = str.substring(0, patternLength).repeat(str.length() / patternLength);
if (candidate.equals(str)) {
return true;
}
}
return false;
}
}