Skip to content

Commit

Permalink
AoC 2024 Day 10 - java
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 10, 2024
1 parent 43efdb4 commit 5f4b90a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | | | | | | | | | | | | | | | | |
<!-- @END:ImplementationsTable:2024@ -->

Expand Down
103 changes: 103 additions & 0 deletions src/main/java/AoC2024_10.java
Original file line number Diff line number Diff line change
@@ -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<IntGrid, Integer, Integer> {

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<String> inputs) {
return IntGrid.from(inputs);
}

private List<List<Cell>> getTrails(final IntGrid grid) {
class BFS {
private final List<List<Cell>> trails = new ArrayList<>();

void bfs(final List<Cell> trail) {
final Deque<List<Cell>> q = new ArrayDeque<>(List.of(trail));
while (!q.isEmpty()) {
final List<Cell> 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<Cell> 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<List<Cell>> 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
""";
}

0 comments on commit 5f4b90a

Please sign in to comment.