Skip to content

Commit 45a27d1

Browse files
day 18
1 parent a019e1c commit 45a27d1

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.adventofcode.aoc2024;
2+
3+
import static com.adventofcode.utils.Utils.itoa;
4+
import static java.util.stream.Collectors.toCollection;
5+
6+
import com.adventofcode.Solution;
7+
import com.adventofcode.utils.Direction;
8+
import com.adventofcode.utils.GraphUtils;
9+
import com.adventofcode.utils.Point;
10+
import com.adventofcode.utils.Utils;
11+
import java.util.Collection;
12+
import java.util.HashSet;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.Set;
16+
import java.util.stream.Stream;
17+
18+
class AoC182024 implements Solution {
19+
20+
private static final int EXAMPLE_SIZE = 7;
21+
private static final int INPUT_SIZE = 71;
22+
23+
@Override
24+
public String solveFirstPart(final Stream<String> input) {
25+
return solve( input, true );
26+
}
27+
28+
@Override
29+
public String solveSecondPart(final Stream<String> input) {
30+
return solve( input, false );
31+
}
32+
33+
private String solve(final Stream<String> input, final boolean first) {
34+
List<String> lines = input.toList();
35+
boolean isExample = lines.size() < INPUT_SIZE;
36+
int maxCorruption = isExample ? 12 : 1024;
37+
int size = isExample ? EXAMPLE_SIZE : INPUT_SIZE;
38+
Point start = new Point( 0, 0 );
39+
Point end = new Point( size - 1, size - 1 );
40+
Set<Point> corruptedPoints = lines.stream()
41+
.limit( maxCorruption - 1 )
42+
.map( this::getCorrupted )
43+
.collect( toCollection( HashSet::new ) );
44+
for ( int i = maxCorruption - 1; i < lines.size(); i++ ) {
45+
Point corrupted = getCorrupted( lines.get( i ) );
46+
corruptedPoints.add( corrupted );
47+
Map<Point, Long> distances = GraphUtils.computeShortestPaths( start, n -> n.equals( end ),
48+
(Point n) -> getNeighbours( n, corruptedPoints, size ) );
49+
if ( first ) {
50+
return itoa( distances.get( end ) );
51+
} else if ( !distances.containsKey( end ) ) {
52+
return corrupted.i() + "," + corrupted.j();
53+
}
54+
}
55+
throw new IllegalArgumentException();
56+
}
57+
58+
private Point getCorrupted(final String line) {
59+
List<Long> numbers = Utils.toLongList( line );
60+
return new Point( numbers.get( 0 ).intValue(), numbers.get( 1 ).intValue() );
61+
}
62+
63+
private Collection<Point> getNeighbours(final Point curr, final Set<Point> corruptedPoints,
64+
final int size) {
65+
return Stream.of( Direction.values() )
66+
.map( curr::move )
67+
.filter( n -> 0 <= n.i() && n.i() < size && 0 <= n.j() && n.j() < size )
68+
.filter( n -> !corruptedPoints.contains( n ) )
69+
.toList();
70+
}
71+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.adventofcode.aoc2024;
2+
3+
import static com.adventofcode.AbstractSolutionTest.Type.FIRST;
4+
import static com.adventofcode.AbstractSolutionTest.Type.SECOND;
5+
6+
import com.adventofcode.AbstractSolutionTest;
7+
import com.adventofcode.Solution;
8+
import java.util.List;
9+
import org.junit.runner.RunWith;
10+
import org.junit.runners.Parameterized;
11+
import org.junit.runners.Parameterized.Parameters;
12+
13+
@RunWith(Parameterized.class)
14+
public class AoC182024Test extends AbstractSolutionTest {
15+
16+
private static final Solution INSTANCE = new AoC182024();
17+
18+
public AoC182024Test(final Type type, final String input, final String result) {
19+
super( INSTANCE, type, input, result );
20+
}
21+
22+
@Parameters(name = PARAMETERS_MESSAGE)
23+
public static Iterable<Object[]> data() {
24+
return List.of( new Object[][]{
25+
{FIRST, """
26+
5,4
27+
4,2
28+
4,5
29+
3,0
30+
2,1
31+
6,3
32+
2,4
33+
1,5
34+
0,6
35+
3,3
36+
2,6
37+
5,1
38+
1,2
39+
5,5
40+
2,5
41+
6,5
42+
1,4
43+
0,4
44+
6,4
45+
1,1
46+
6,1
47+
1,0
48+
0,5
49+
1,6
50+
2,0
51+
""", "22"},
52+
{FIRST, getInput( INSTANCE ), "268"},
53+
{SECOND, """
54+
5,4
55+
4,2
56+
4,5
57+
3,0
58+
2,1
59+
6,3
60+
2,4
61+
1,5
62+
0,6
63+
3,3
64+
2,6
65+
5,1
66+
1,2
67+
5,5
68+
2,5
69+
6,5
70+
1,4
71+
0,4
72+
6,4
73+
1,1
74+
6,1
75+
1,0
76+
0,5
77+
1,6
78+
2,0
79+
""", "6,1"},
80+
{SECOND, getInput( INSTANCE ), "64,11"}
81+
} );
82+
}
83+
}
22.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)