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 ;
61import com .github .pareronia .aoc .solution .Sample ;
72import com .github .pareronia .aoc .solution .Samples ;
83import com .github .pareronia .aoc .solution .SolutionBase ;
94
10- import java .util .HashMap ;
5+ import java .util .Arrays ;
116import java .util .List ;
12- import java .util .Map ;
13- import java .util .Set ;
14- import java .util .stream .Stream ;
157
168@ SuppressWarnings ({"PMD.ClassNamingConventions" , "PMD.NoPackage" })
17- public final class AoC2025_07 extends SolutionBase <CharGrid , Long , Long > {
9+ public final class AoC2025_07 extends SolutionBase <List < String > , Long , Long > {
1810
1911 public static final char SPLITTER = '^' ;
2012 public static final char START = 'S' ;
@@ -32,68 +24,41 @@ public static AoC2025_07 createDebug() {
3224 }
3325
3426 @ Override
35- protected CharGrid parseInput (final List <String > inputs ) {
36- return CharGrid . from ( inputs ) ;
27+ protected List < String > parseInput (final List <String > inputs ) {
28+ return inputs ;
3729 }
3830
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 ());
31+ @ SuppressWarnings ("PMD.AvoidInstantiatingObjectsInLoops" )
32+ private Result solve (final List <String > grid ) {
33+ final int start = grid .getFirst ().indexOf (START );
34+ long [] beams = new long [grid .getFirst ().length ()];
35+ beams [start ] = 1 ;
36+ long splits = 0 ;
37+ for (int r = 1 ; r < grid .size (); r ++) {
38+ final char [] row = grid .get (r ).toCharArray ();
39+ final long [] newBeams = new long [row .length ];
40+ for (int c = 0 ; c < row .length ; c ++) {
41+ if (row [c ] == SPLITTER ) {
42+ newBeams [c - 1 ] += beams [c ];
43+ newBeams [c + 1 ] += beams [c ];
44+ splits += (beams [c ] > 0 ? 1 : 0 );
45+ } else {
46+ newBeams [c ] += beams [c ];
5047 }
5148 }
49+ beams = newBeams ;
5250 }
53- return ans ;
51+ return new Result ( splits , Arrays . stream ( beams ). sum ()) ;
5452 }
5553
5654 @ 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- }
55+ public Long solvePart1 (final List <String > grid ) {
56+ return this .solve (grid ).splits ;
57+ }
9358
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 () ;
59+ @ Override
60+ public Long solvePart2 ( final List < String > grid ) {
61+ return this . solve ( grid ). beams ;
9762 }
9863
9964 @ Samples ({
@@ -123,4 +88,6 @@ public static void main(final String[] args) throws Exception {
12388 .^.^.^.^.^...^.
12489 ...............
12590 """ ;
91+
92+ private record Result (long splits , long beams ) {}
12693}
0 commit comments