From e79e352d7ac5c7707f8cd1f48aacfc662eab15d3 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Fri, 5 Dec 2025 22:52:49 -0500 Subject: [PATCH] day five in the books --- src/Day05.java | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/Day05.java diff --git a/src/Day05.java b/src/Day05.java new file mode 100644 index 0000000..9652f37 --- /dev/null +++ b/src/Day05.java @@ -0,0 +1,100 @@ +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 Day05 { + 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/05/input"))) { + List> ranges = new ArrayList<>(); + List ingredients = new ArrayList<>(); + + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + if (line.contains("-")) { + String[] parts = line.split("-"); + long first = Long.parseLong(parts[0]); + long second = Long.parseLong(parts[1]); + ranges.add(new Pair<>(first, second)); + } else if (!line.isBlank()) { + ingredients.add(Long.parseLong(line)); + } + } + int count = 0; + for (Long ingredient : ingredients) { + for (Pair range : ranges) { + if (ingredient >= range.head && ingredient <= range.tail) { + count++; + break; + } + } + } + IO.println(count); + } catch (Exception e) { + IO.println(e.getMessage()); + } + } + + private static void part2() { + try (Scanner scanner = new Scanner(new FileReader("resources/inputs/05/input"))) { + List> ranges = new ArrayList<>(); + + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + if (line.contains("-")) { + String[] parts = line.split("-"); + long first = Long.parseLong(parts[0]); + long second = Long.parseLong(parts[1]); + ranges.add(new Pair<>(first, second)); + } + } + Pair result = ranges.stream() + .sorted((a, b) -> Long.compare(a.head, b.head)) + .reduce( + // first is count, second is end of range covered so far + new Pair<>(0L, 0L), + (total, curr) -> { + if (total.tail >= curr.tail) { + // this range was subsumed by a previous range + return total; + } + if (total.tail >= curr.head) { + // this range was partially included + return new Pair<>(total.head + (curr.tail - total.tail), curr.tail); + } + // this range was not previously included at all + // +1 to include both endpoints + return new Pair<>(total.head + (curr.tail - curr.head) + 1, curr.tail); + }, + // merge function doesn't actually get used here but might as well write it + (a, b) -> new Pair<>(a.head + b.head, Long.max(a.tail, b.tail))); + IO.println(result.head); + } catch (Exception e) { + IO.println(e.getMessage()); + } + } + + static class Pair { + public final T head; + public final U tail; + + public Pair(T first, U second) { + this.head = first; + this.tail = second; + } + } +}