just needed to be smarter about which batteries were valuable to remove

This commit is contained in:
2025-12-03 22:04:23 -05:00
parent d8ba6085a9
commit d444a08d2a

View File

@@ -42,7 +42,7 @@ public class Day03 {
} }
private static void part2() { private static void part2() {
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/03/testinput"))) { try (Scanner scanner = new Scanner(new FileReader("resources/inputs/03/input"))) {
long sum = 0L; long sum = 0L;
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
String raw = scanner.nextLine(); String raw = scanner.nextLine();
@@ -52,17 +52,16 @@ public class Day03 {
curr.addAll(bank.subList(0, 12)); curr.addAll(bank.subList(0, 12));
for (int i = 12; i < bank.size(); i++) { for (int i = 12; i < bank.size(); i++) {
curr.add(bank.get(i)); // add the next battery to the end of the list curr.add(bank.get(i)); // add the next battery to the end of the list
for (int j = 1; j < 10; j++) { // find the smallest battery in the list that's ahead of a larger battery
// does the list contain a battery of the specified value? int t1 = 0;
int index = curr.indexOf(String.valueOf(j)); int t2 = 1;
if (index != -1) { while (t2 < curr.size() && Integer.parseInt(curr.get(t1)) >= Integer.parseInt(curr.get(t2))) {
curr.remove(index); // remove it if it does t1++;
break; t2++;
}
} }
curr.remove(t1);
} }
String result = curr.stream().reduce("", String::concat); String result = curr.stream().reduce("", String::concat);
IO.println(raw + " " + result);
sum += Long.parseLong(result); sum += Long.parseLong(result);
} }
IO.println(sum); IO.println(sum);