Skip to content

Commit 842ba3f

Browse files
committed
AoC 2025 Day 7 - java
1 parent 2eeeab2 commit 842ba3f

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
1010
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
1111
| python3 | [](src/main/python/AoC2025_01.py) | [](src/main/python/AoC2025_02.py) | [](src/main/python/AoC2025_03.py) | [](src/main/python/AoC2025_04.py) | [](src/main/python/AoC2025_05.py) | [](src/main/python/AoC2025_06.py) | [](src/main/python/AoC2025_07.py) | | | | | |
12-
| java | [](src/main/java/AoC2025_01.java) | [](src/main/java/AoC2025_02.java) | [](src/main/java/AoC2025_03.java) | [](src/main/java/AoC2025_04.java) | [](src/main/java/AoC2025_05.java) | [](src/main/java/AoC2025_06.java) | | | | | | |
12+
| java | [](src/main/java/AoC2025_01.java) | [](src/main/java/AoC2025_02.java) | [](src/main/java/AoC2025_03.java) | [](src/main/java/AoC2025_04.java) | [](src/main/java/AoC2025_05.java) | [](src/main/java/AoC2025_06.java) | [](src/main/java/AoC2025_07.java) | | | | | |
1313
| bash | [](src/main/bash/AoC2025_01.sh) | [](src/main/bash/AoC2025_02.sh) | [](src/main/bash/AoC2025_03.sh) | [](src/main/bash/AoC2025_04.sh) | [](src/main/bash/AoC2025_05.sh) | [](src/main/bash/AoC2025_06.sh) | | | | | | |
1414
<!-- @END:ImplementationsTable:2025@ -->
1515

src/main/java/AoC2025_07.java

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import static java.util.stream.Collectors.toSet;
2+
3+
import com.github.pareronia.aoc.CharGrid;
4+
import com.github.pareronia.aoc.Grid.Cell;
5+
import com.github.pareronia.aoc.geometry.Direction;
6+
import com.github.pareronia.aoc.solution.Sample;
7+
import com.github.pareronia.aoc.solution.Samples;
8+
import com.github.pareronia.aoc.solution.SolutionBase;
9+
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Set;
14+
import java.util.stream.Stream;
15+
16+
@SuppressWarnings({"PMD.ClassNamingConventions", "PMD.NoPackage"})
17+
public final class AoC2025_07 extends SolutionBase<CharGrid, Long, Long> {
18+
19+
public static final char SPLITTER = '^';
20+
public static final char START = 'S';
21+
22+
private AoC2025_07(final boolean debug) {
23+
super(debug);
24+
}
25+
26+
public static AoC2025_07 create() {
27+
return new AoC2025_07(false);
28+
}
29+
30+
public static AoC2025_07 createDebug() {
31+
return new AoC2025_07(true);
32+
}
33+
34+
@Override
35+
protected CharGrid parseInput(final List<String> inputs) {
36+
return CharGrid.from(inputs);
37+
}
38+
39+
@Override
40+
public Long solvePart1(final CharGrid grid) {
41+
final Set<Integer> beams = grid.getAllEqualTo(START).map(Cell::getCol).collect(toSet());
42+
long ans = 0L;
43+
final Iterable<Cell> splitters = () -> grid.getAllEqualTo(SPLITTER).iterator();
44+
for (final Cell splitter : splitters) {
45+
if (beams.contains(splitter.getCol())) {
46+
ans++;
47+
beams.remove(splitter.getCol());
48+
for (final Direction direction : Set.of(Direction.LEFT, Direction.RIGHT)) {
49+
beams.add(splitter.at(direction).getCol());
50+
}
51+
}
52+
}
53+
return ans;
54+
}
55+
56+
@Override
57+
public Long solvePart2(final CharGrid grid) {
58+
class DFS {
59+
private final Cell start;
60+
private final Set<Cell> splitters;
61+
private final Map<Cell, Long> cache = new HashMap<>();
62+
63+
DFS() {
64+
this.start = grid.getAllEqualTo(START).findFirst().orElseThrow();
65+
this.splitters = grid.getAllEqualTo(SPLITTER).collect(toSet());
66+
splitters.add(this.start);
67+
}
68+
69+
long dfs(final Cell cell) {
70+
final long ans;
71+
if (this.cache.containsKey(cell)) {
72+
ans = this.cache.get(cell);
73+
} else {
74+
if (cell.equals(this.start)) {
75+
ans = 1;
76+
} else {
77+
ans =
78+
grid.getCellsN(cell)
79+
.takeWhile(n -> !this.splitters.contains(n))
80+
.flatMap(
81+
n ->
82+
Stream.of(Direction.LEFT, Direction.RIGHT)
83+
.map(n::at))
84+
.filter(this.splitters::contains)
85+
.mapToLong(this::dfs)
86+
.sum();
87+
}
88+
this.cache.put(cell, ans);
89+
}
90+
return ans;
91+
}
92+
}
93+
94+
final Cell bottomLeft = Cell.at(grid.getMaxRowIndex(), 0);
95+
final DFS dfs = new DFS();
96+
return dfs.dfs(bottomLeft) + grid.getCellsE(bottomLeft).mapToLong(dfs::dfs).sum();
97+
}
98+
99+
@Samples({
100+
@Sample(method = "part1", input = TEST, expected = "21"),
101+
@Sample(method = "part2", input = TEST, expected = "40"),
102+
})
103+
public static void main(final String[] args) throws Exception {
104+
create().run();
105+
}
106+
107+
private static final String TEST =
108+
"""
109+
.......S.......
110+
...............
111+
.......^.......
112+
...............
113+
......^.^......
114+
...............
115+
.....^.^.^.....
116+
...............
117+
....^.^...^....
118+
...............
119+
...^.^...^.^...
120+
...............
121+
..^...^.....^..
122+
...............
123+
.^.^.^.^.^...^.
124+
...............
125+
""";
126+
}

0 commit comments

Comments
 (0)