package src; import java.io.FileReader; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Day03 { public static void main(String[] args) { Instant start = Instant.now(); part1(); Instant middle = Instant.now(); part2(); Instant end = Instant.now(); IO.println("part 1: " + Duration.between(start, middle).toMillis() + " ms"); IO.println("part 2: " + Duration.between(middle, end).toMillis() + " ms"); } private static void part1() { try (Scanner scanner = new Scanner(new FileReader("resources/inputs/03/input"))) { int sum = 0; while (scanner.hasNextLine()) { String raw = scanner.nextLine(); List bank = List.of(raw.split("")); int a = 0; int b = 1; int curr = Integer.parseInt(bank.get(a) + bank.get(b)); for (int i = 2; i < bank.size(); i++) { int candidateA = Integer.parseInt(bank.get(a) + bank.get(i)); int candidateB = Integer.parseInt(bank.get(b) + bank.get(i)); if (candidateA > curr) { curr = candidateA; b = i; } if (candidateB > curr) { curr = candidateB; a = b; b = i; } } sum += curr; } IO.println(sum); } catch (Exception e) { IO.println(e.getMessage()); } } private static void part2() { try (Scanner scanner = new Scanner(new FileReader("resources/inputs/03/input"))) { long sum = 0L; while (scanner.hasNextLine()) { String raw = scanner.nextLine(); List bank = List.of(raw.split("")); // first, get the initial 12 batteries List curr = new ArrayList<>(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 // 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); } sum += Long.parseLong(String.join("", curr)); } IO.println(sum); } catch (Exception e) { IO.println(e.getMessage()); } } }