Skip to content

Commit 612fce1

Browse files
committed
[Year 2024 Day 22 Part 2] ok
1 parent 576c679 commit 612fce1

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

solutions/src/main/java/dev/vinyard/adventofcode/soluce/year2023/day22/ASD.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import org.apache.commons.geometry.euclidean.threed.Bounds3D;
88
import org.apache.commons.geometry.euclidean.threed.Vector3D;
99

10+
import java.util.HashSet;
1011
import java.util.LinkedList;
1112
import java.util.List;
13+
import java.util.Set;
14+
import java.util.stream.Collectors;
1215

1316
public class ASD {
1417

@@ -24,6 +27,15 @@ public long fall() {
2427
bricks.forEach(Brick::fall);
2528
return bricks.stream().filter(b -> b.getFallingOnBricks().isEmpty()).count();
2629
}
30+
31+
public Long part2() {
32+
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();
38+
}
2739
}
2840

2941
// Classe proxy pour Brick avec cache Caffeine
@@ -101,6 +113,14 @@ public List<Brick> getFallingOnBricks() {
101113
.toList();
102114
}
103115

116+
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;
122+
}
123+
104124
public Brick(Bounds3D bounds, LinkedList<Brick> bricks) {
105125
this.bounds = bounds;
106126
this.bricks = bricks;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dev.vinyard.adventofcode.soluce.year2023.day22;
2+
3+
import dev.vinyard.aoc.plugins.solution.api.Solution;
4+
import dev.vinyard.aoc.plugins.solution.api.annotation.AdventOfCodeSolution;
5+
import org.antlr.v4.runtime.CharStream;
6+
import org.antlr.v4.runtime.CharStreams;
7+
import org.antlr.v4.runtime.CommonTokenStream;
8+
9+
@AdventOfCodeSolution(year = 2023, day = 22, part = 2, description = "Sand Slabs", link = "https://adventofcode.com/2023/day/22", tags = "unsolved")
10+
public class Day22Part2Solution implements Solution<Long> {
11+
12+
/**
13+
* <h2>--- Part Two ---</h2>
14+
* <p>Disintegrating bricks one at a time isn't going to be fast enough. While it might sound dangerous, what you really need is a <b>chain reaction</b>.</p>
15+
* <p>You'll need to figure out the best brick to disintegrate. For each brick, determine how many <b>other bricks would fall</b> if that brick were disintegrated.</p>
16+
* <p>Using the same example as above:</p>
17+
* <ul>
18+
* <li>Disintegrating brick A would cause all <b>6</b> other bricks to fall.</li>
19+
* <li>Disintegrating brick F would cause only <b>1</b> other brick, G, to fall.</li>
20+
* </ul>
21+
* <p>Disintegrating any other brick would cause <b>no other bricks</b> to fall. So, in this example, the sum of <b>the number of other bricks that would fall</b> as a result of disintegrating each brick is <b>7</b>.</p>
22+
* <p>For each brick, determine how many <b>other bricks</b> would fall if that brick were disintegrated. <b>What is the sum of the number of other bricks that would fall?</b></p>
23+
*/
24+
@Override
25+
public Long solve(String input) {
26+
CharStream charStream = CharStreams.fromString(input);
27+
28+
SolutionLexer lexer = new SolutionLexer(charStream);
29+
CommonTokenStream tokens = new CommonTokenStream(lexer);
30+
SolutionParser parser = new SolutionParser(tokens);
31+
32+
ASD.Root root = parser.root().out;
33+
34+
return root.part2();
35+
}
36+
}

solutions/src/test/java/dev/vinyard/adventofcode/soluce/year2023/day22/Day22Part1SolutionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public Solution<Long> getSolution() {
1616
@Override
1717
public Stream<Arguments> testSolution() {
1818
return Stream.of(
19-
Arguments.of("soluce/year2023/day22/part1/test.txt", 5L, null, null),
19+
Arguments.of("soluce/year2023/day22/test.txt", 5L, null, null),
2020
Arguments.of("soluce/year2023/day22/input.txt", 443L, null, null)
2121
);
2222
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dev.vinyard.adventofcode.soluce.year2023.day22;
2+
3+
import dev.vinyard.adventofcode.common.BaseTest;
4+
import dev.vinyard.aoc.plugins.solution.api.Solution;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
7+
import java.util.stream.Stream;
8+
9+
public class Day22Part2SolutionTest extends BaseTest<Long> {
10+
11+
@Override
12+
public Solution<Long> getSolution() {
13+
return new Day22Part2Solution();
14+
}
15+
16+
@Override
17+
public Stream<Arguments> testSolution() {
18+
return Stream.of(
19+
Arguments.of("soluce/year2023/day22/test.txt", 7L, null, null),
20+
Arguments.of("soluce/year2023/day22/input.txt", 69915L, null, null)
21+
);
22+
}
23+
}

0 commit comments

Comments
 (0)