diff --git a/README.md b/README.md index 564f8415..dd1628a1 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) | [✓](src/main/python/AoC2024_04.py) | [✓](src/main/python/AoC2024_05.py) | [✓](src/main/python/AoC2024_06.py) | [✓](src/main/python/AoC2024_07.py) | [✓](src/main/python/AoC2024_08.py) | [✓](src/main/python/AoC2024_09.py) | [✓](src/main/python/AoC2024_10.py) | | | | | | | | | | | | | | | | -| java | [✓](src/main/java/AoC2024_01.java) | [✓](src/main/java/AoC2024_02.java) | [✓](src/main/java/AoC2024_03.java) | [✓](src/main/java/AoC2024_04.java) | [✓](src/main/java/AoC2024_05.java) | [✓](src/main/java/AoC2024_06.java) | [✓](src/main/java/AoC2024_07.java) | [✓](src/main/java/AoC2024_08.java) | | | | | | | | | | | | | | | | | | +| java | [✓](src/main/java/AoC2024_01.java) | [✓](src/main/java/AoC2024_02.java) | [✓](src/main/java/AoC2024_03.java) | [✓](src/main/java/AoC2024_04.java) | [✓](src/main/java/AoC2024_05.java) | [✓](src/main/java/AoC2024_06.java) | [✓](src/main/java/AoC2024_07.java) | [✓](src/main/java/AoC2024_08.java) | | [✓](src/main/java/AoC2024_10.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) | [✓](src/main/rust/AoC2024_04/src/main.rs) | [✓](src/main/rust/AoC2024_05/src/main.rs) | [✓](src/main/rust/AoC2024_06/src/main.rs) | [✓](src/main/rust/AoC2024_07/src/main.rs) | [✓](src/main/rust/AoC2024_08/src/main.rs) | | | | | | | | | | | | | | | | | | diff --git a/src/main/java/AoC2024_10.java b/src/main/java/AoC2024_10.java new file mode 100644 index 00000000..ed878964 --- /dev/null +++ b/src/main/java/AoC2024_10.java @@ -0,0 +1,103 @@ +import static com.github.pareronia.aoc.Utils.last; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.List; + +import com.github.pareronia.aoc.Grid.Cell; +import com.github.pareronia.aoc.IntGrid; +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_10 extends SolutionBase { + + private AoC2024_10(final boolean debug) { + super(debug); + } + + public static AoC2024_10 create() { + return new AoC2024_10(false); + } + + public static AoC2024_10 createDebug() { + return new AoC2024_10(true); + } + + @Override + protected IntGrid parseInput(final List inputs) { + return IntGrid.from(inputs); + } + + private List> getTrails(final IntGrid grid) { + class BFS { + private final List> trails = new ArrayList<>(); + + void bfs(final List trail) { + final Deque> q = new ArrayDeque<>(List.of(trail)); + while (!q.isEmpty()) { + final List curr = q.pop(); + if (curr.size() == 10) { + trails.add(curr); + continue; + } + final int next = grid.getValue(last(curr)) + 1; + grid.getCapitalNeighbours(last(curr)) + .filter(n -> grid.getValue(n) == next) + .forEach(n -> { + final List newTrail = new ArrayList<>(curr); + newTrail.add(n); + q.add(newTrail); + }); + } + } + } + + final BFS bfs = new BFS(); + grid.getAllEqualTo(0).forEach(zero -> bfs.bfs(List.of(zero))); + return bfs.trails; + } + + @Override + public Integer solvePart1(final IntGrid grid) { + final List> trails = getTrails(grid); + return trails.stream() + .map(trail -> trail.get(0)) + .distinct() + .mapToInt(zero -> (int) trails.stream() + .filter(trail -> trail.get(0).equals(zero)) + .map(trail -> trail.get(9)) + .distinct() + .count()) + .sum(); + } + + @Override + public Integer solvePart2(final IntGrid grid) { + return getTrails(grid).size(); + } + + @Override + @Samples({ + @Sample(method = "part1", input = TEST, expected = "36"), + @Sample(method = "part2", input = TEST, expected = "81"), + }) + public void samples() { + } + + public static void main(final String[] args) throws Exception { + AoC2024_10.create().run(); + } + + private static final String TEST = """ + 89010123 + 78121874 + 87430965 + 96549874 + 45678903 + 32019012 + 01329801 + 10456732 + """; +} \ No newline at end of file