Skip to content

Commit 673bc48

Browse files
day 25
1 parent da81aba commit 673bc48

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.adventofcode.aoc2024;
2+
3+
4+
import static com.adventofcode.utils.Utils.HASH;
5+
import static com.adventofcode.utils.Utils.MERRY_CHRISTMAS;
6+
import static com.adventofcode.utils.Utils.itoa;
7+
8+
import com.adventofcode.Solution;
9+
import java.util.ArrayList;
10+
import java.util.Iterator;
11+
import java.util.List;
12+
import java.util.stream.Stream;
13+
14+
class AoC252024 implements Solution {
15+
16+
@Override
17+
public String solveFirstPart(final Stream<String> input) {
18+
return solve( input, true );
19+
}
20+
21+
@Override
22+
public String solveSecondPart(final Stream<String> input) {
23+
return solve( input, false );
24+
}
25+
26+
private String solve(final Stream<String> input, final boolean first) {
27+
if ( !first ) {
28+
return MERRY_CHRISTMAS;
29+
}
30+
List<int[]> locks = new ArrayList<>();
31+
List<int[]> keys = new ArrayList<>();
32+
var itr = input.iterator();
33+
while ( itr.hasNext() ) {
34+
String firstLine = itr.next();
35+
if ( firstLine.isEmpty() ) {
36+
continue;
37+
}
38+
if ( firstLine.charAt( 0 ) == HASH ) {
39+
locks.add( getPins( itr, false ) );
40+
} else {
41+
keys.add( getPins( itr, true ) );
42+
}
43+
}
44+
return itoa( keys.stream()
45+
.mapToLong( key -> locks.stream().filter( lock -> fits( key, lock ) ).count() )
46+
.sum() );
47+
}
48+
49+
private int[] getPins(final Iterator<String> itr, final boolean isKey) {
50+
int[] pins = new int[5];
51+
for ( int i = 0; i < 6; i++ ) {
52+
String line = itr.next();
53+
for ( int j = 0; !(isKey && i == 5) && (j < pins.length); j++ ) {
54+
if ( line.charAt( j ) == HASH ) {
55+
pins[j]++;
56+
}
57+
}
58+
}
59+
return pins;
60+
}
61+
62+
private boolean fits(int[] key, int[] lock) {
63+
for ( int i = 0; i < lock.length; i++ ) {
64+
if ( lock[i] + key[i] > 5 ) {
65+
return false;
66+
}
67+
}
68+
return true;
69+
}
70+
}
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.AbstractSolutionTest.Type.FIRST;
4+
import static com.adventofcode.AbstractSolutionTest.Type.SECOND;
5+
import static com.adventofcode.utils.Utils.MERRY_CHRISTMAS;
6+
7+
import com.adventofcode.AbstractSolutionTest;
8+
import com.adventofcode.Solution;
9+
import java.util.List;
10+
import org.junit.runner.RunWith;
11+
import org.junit.runners.Parameterized;
12+
import org.junit.runners.Parameterized.Parameters;
13+
14+
@RunWith(Parameterized.class)
15+
public class AoC252024Test extends AbstractSolutionTest {
16+
17+
private static final Solution INSTANCE = new AoC252024();
18+
19+
public AoC252024Test(final Type type, final String input, final String result) {
20+
super( INSTANCE, type, input, result );
21+
}
22+
23+
@Parameters(name = PARAMETERS_MESSAGE)
24+
public static Iterable<Object[]> data() {
25+
return List.of( new Object[][]{
26+
{FIRST, """
27+
#####
28+
.####
29+
.####
30+
.####
31+
.#.#.
32+
.#...
33+
.....
34+
35+
#####
36+
##.##
37+
.#.##
38+
...##
39+
...#.
40+
...#.
41+
.....
42+
43+
.....
44+
#....
45+
#....
46+
#...#
47+
#.#.#
48+
#.###
49+
#####
50+
51+
.....
52+
.....
53+
#.#..
54+
###..
55+
###.#
56+
###.#
57+
#####
58+
59+
.....
60+
.....
61+
.....
62+
#....
63+
#.#..
64+
#.#.#
65+
#####
66+
""", "3"},
67+
{FIRST, getInput( INSTANCE ), "2691"},
68+
{SECOND, getInput( INSTANCE ), MERRY_CHRISTMAS}
69+
} );
70+
}
71+
}
24.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)