just needed to be smarter about which batteries were valuable to remove
This commit is contained in:
@@ -42,7 +42,7 @@ public class Day03 {
|
||||
}
|
||||
|
||||
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;
|
||||
while (scanner.hasNextLine()) {
|
||||
String raw = scanner.nextLine();
|
||||
@@ -52,17 +52,16 @@ public class Day03 {
|
||||
curr.addAll(bank.subList(0, 12));
|
||||
for (int i = 12; i < bank.size(); i++) {
|
||||
curr.add(bank.get(i)); // add the next battery to the end of the list
|
||||
for (int j = 1; j < 10; j++) {
|
||||
// does the list contain a battery of the specified value?
|
||||
int index = curr.indexOf(String.valueOf(j));
|
||||
if (index != -1) {
|
||||
curr.remove(index); // remove it if it does
|
||||
break;
|
||||
}
|
||||
// find the smallest battery in the list that's ahead of a larger battery
|
||||
int t1 = 0;
|
||||
int t2 = 1;
|
||||
while (t2 < curr.size() && Integer.parseInt(curr.get(t1)) >= Integer.parseInt(curr.get(t2))) {
|
||||
t1++;
|
||||
t2++;
|
||||
}
|
||||
curr.remove(t1);
|
||||
}
|
||||
String result = curr.stream().reduce("", String::concat);
|
||||
IO.println(raw + " " + result);
|
||||
sum += Long.parseLong(result);
|
||||
}
|
||||
IO.println(sum);
|
||||
|
||||
Reference in New Issue
Block a user