From 7fcaf6d3b0b600e8ef3b1ef6f3fa638248f77efb Mon Sep 17 00:00:00 2001 From: pareronia <49491686+pareronia@users.noreply.github.com> Date: Wed, 4 Dec 2024 01:16:24 +0100 Subject: [PATCH] AoC 2024 Day 2 - java --- README.md | 2 +- src/main/java/AoC2024_02.java | 84 +++++++++++++++++++ .../com/github/pareronia/aoc/IterTools.java | 21 +++++ .../pareronia/aoc/IterToolsTestCase.java | 11 +++ 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/main/java/AoC2024_02.java diff --git a/README.md b/README.md index 834d11af..128cc2d6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | | ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | python3 | [✓](src/main/python/AoC2024_01.py) | [✓](src/main/python/AoC2024_02.py) | [✓](src/main/python/AoC2024_03.py) | | | | | | | | | | | | | | | | | | | | | | | -| java | [✓](src/main/java/AoC2024_01.java) | | [✓](src/main/java/AoC2024_03.java) | | | | | | | | | | | | | | | | | | | | | | | +| java | [✓](src/main/java/AoC2024_01.java) | [✓](src/main/java/AoC2024_02.java) | [✓](src/main/java/AoC2024_03.java) | | | | | | | | | | | | | | | | | | | | | | | | rust | [✓](src/main/rust/AoC2024_01/src/main.rs) | [✓](src/main/rust/AoC2024_02/src/main.rs) | [✓](src/main/rust/AoC2024_03/src/main.rs) | | | | | | | | | | | | | | | | | | | | | | | diff --git a/src/main/java/AoC2024_02.java b/src/main/java/AoC2024_02.java new file mode 100644 index 00000000..33b8e2ad --- /dev/null +++ b/src/main/java/AoC2024_02.java @@ -0,0 +1,84 @@ +import static com.github.pareronia.aoc.IntegerSequence.Range.range; +import static com.github.pareronia.aoc.IterTools.windows; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.github.pareronia.aoc.Utils; +import com.github.pareronia.aoc.solution.Sample; +import com.github.pareronia.aoc.solution.Samples; +import com.github.pareronia.aoc.solution.SolutionBase; + +public final class AoC2024_02 + extends SolutionBase>, Integer, Integer> { + + private AoC2024_02(final boolean debug) { + super(debug); + } + + public static AoC2024_02 create() { + return new AoC2024_02(false); + } + + public static AoC2024_02 createDebug() { + return new AoC2024_02(true); + } + + @Override + protected List> parseInput(final List inputs) { + return inputs.stream() + .map(line -> Arrays.stream(line.split(" ")) + .map(Integer::parseInt) + .toList()) + .toList(); + } + + private boolean safe(final List levels) { + final List diffs = Utils.stream(windows(levels)) + .map(w -> w.second() - w.first()) + .toList(); + return diffs.stream().allMatch(diff -> 1 <= diff && diff <= 3) + || diffs.stream().allMatch(diff -> -1 >= diff && diff >= -3); + } + + @Override + public Integer solvePart1(final List> reports) { + return (int) reports.stream().filter(this::safe).count(); + } + + @Override + public Integer solvePart2(final List> reports) { + return (int) reports.stream() + .filter(report -> + range(report.size()).intStream() + .mapToObj(i -> { + final List tmp = new ArrayList<>(report); + tmp.remove(i); + return tmp; + }) + .anyMatch(this::safe) + ).count(); + } + + @Override + @Samples({ + @Sample(method = "part1", input = TEST, expected = "2"), + @Sample(method = "part2", input = TEST, expected = "4"), + }) + public void samples() { + } + + public static void main(final String[] args) throws Exception { + AoC2024_02.create().run(); + } + + private static final String TEST = """ + 7 6 4 2 1 + 1 2 7 8 9 + 9 7 6 2 1 + 1 3 2 4 5 + 8 6 4 4 1 + 1 3 6 7 9 + """; +} \ No newline at end of file diff --git a/src/main/java/com/github/pareronia/aoc/IterTools.java b/src/main/java/com/github/pareronia/aoc/IterTools.java index ea80696e..67868480 100644 --- a/src/main/java/com/github/pareronia/aoc/IterTools.java +++ b/src/main/java/com/github/pareronia/aoc/IterTools.java @@ -155,6 +155,25 @@ public static Iterator cycle(final Iterable iterable) { return cycle(iterable.iterator()); } + public static Iterator> windows(final List list) { + return new Iterator<>() { + int i = 0; + + @Override + public boolean hasNext() { + return i < list.size() - 1; + } + + @Override + public WindowPair next() { + final WindowPair next + = new WindowPair<>(list.get(i), list.get(i + 1)); + i++; + return next; + } + }; + } + private static final class Heap { public static void accept(final int[] a, final Consumer consumer) { @@ -193,5 +212,7 @@ private static void swap(final int[] a, final int i, final int j) { public record ZippedPair(T first, T second) {} + public record WindowPair(T first, T second) {} + public record Enumerated(int index, T value) {} } diff --git a/src/test/java/com/github/pareronia/aoc/IterToolsTestCase.java b/src/test/java/com/github/pareronia/aoc/IterToolsTestCase.java index 1d0b93fc..186bf866 100644 --- a/src/test/java/com/github/pareronia/aoc/IterToolsTestCase.java +++ b/src/test/java/com/github/pareronia/aoc/IterToolsTestCase.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test; +import com.github.pareronia.aoc.IterTools.WindowPair; import com.github.pareronia.aoc.IterTools.ZippedPair; public class IterToolsTestCase { @@ -78,4 +79,14 @@ public void cycle() { assertThat(ccycle.next()).isEqualTo('c'); } } + + @Test + public void windows() { + final Iterator> windows + = IterTools.windows(List.of(1, 2, 3, 4)); + assertThat(windows.next()).isEqualTo(new WindowPair<>(1, 2)); + assertThat(windows.next()).isEqualTo(new WindowPair<>(2, 3)); + assertThat(windows.next()).isEqualTo(new WindowPair<>(3, 4)); + assertThat(windows.hasNext()).isFalse(); + } }