File tree Expand file tree Collapse file tree 1 file changed +8
-11
lines changed
solutions/src/main/java/dev/vinyard/adventofcode/soluce/year2023/day22 Expand file tree Collapse file tree 1 file changed +8
-11
lines changed Original file line number Diff line number Diff line change 1111import java .util .LinkedList ;
1212import java .util .List ;
1313import java .util .Set ;
14- import java .util .stream .Collectors ;
1514
1615public class ASD {
1716
@@ -30,11 +29,7 @@ public long fall() {
3029
3130 public Long part2 () {
3231 bricks .forEach (Brick ::fall );
33- return bricks .stream ().mapToLong (b -> {
34- Set <Brick > brickSet = new HashSet <>();
35- brickSet .add (b );
36- return b .getAllFallingOnBricks (brickSet ).size ();
37- }).sum ();
32+ return bricks .stream ().mapToLong (b -> b .getAllFallingOnBricks (new HashSet <>()).size () - 1 ).sum ();
3833 }
3934 }
4035
@@ -114,11 +109,13 @@ public List<Brick> getFallingOnBricks() {
114109 }
115110
116111 public Set <Brick > getAllFallingOnBricks (Set <Brick > bricks ) {
117- Set <Brick > supportingBricks = new HashSet <>(getSupportingBricks ());
118- Set <Brick > fallingOnBricks = supportingBricks .stream ().filter (s -> bricks .containsAll (s .getSupportedByBricks ())).collect (Collectors .toSet ());
119- bricks .addAll (fallingOnBricks );
120- fallingOnBricks .addAll (fallingOnBricks .stream ().map (b -> b .getAllFallingOnBricks (bricks )).flatMap (Set ::stream ).collect (Collectors .toSet ()));
121- return fallingOnBricks ;
112+ bricks .add (this );
113+
114+ getSupportingBricks ().stream ()
115+ .filter (b -> bricks .containsAll (b .getSupportedByBricks ()))
116+ .forEach (b -> b .getAllFallingOnBricks (bricks ));
117+
118+ return bricks ;
122119 }
123120
124121 public Brick (Bounds3D bounds , LinkedList <Brick > bricks ) {
You can’t perform that action at this time.
0 commit comments