Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6e2da35

Browse files
day 25 part 1
1 parent de95d6b commit 6e2da35

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.codefork.aoc2024.day25;
2+
3+
import com.codefork.aoc2024.Problem;
4+
import com.codefork.aoc2024.util.Assert;
5+
import com.codefork.aoc2024.util.Grid;
6+
import com.codefork.aoc2024.util.WithIndex;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Set;
12+
import java.util.stream.IntStream;
13+
import java.util.stream.Stream;
14+
15+
public class Part01 extends Problem {
16+
17+
public record Position(int x, int y) {
18+
19+
}
20+
21+
public record Schematic(Set<Position> positions) {
22+
23+
public List<Integer> getHeights() {
24+
return IntStream.range(0, 5)
25+
.boxed()
26+
.map(x -> (int) positions().stream().filter(p -> p.x() == x).count())
27+
.toList();
28+
}
29+
30+
}
31+
32+
record LockAndKey(Schematic lock, Schematic key) {
33+
34+
}
35+
36+
public String solve(Stream<String> data) {
37+
var schematics = Grid.parse(data,
38+
() -> new ArrayList<Schematic>(),
39+
(acc, x, y, ch) -> {
40+
y = y % 8;
41+
if(x == 0 && y == 0) {
42+
acc.add(new Schematic(new HashSet<>()));
43+
}
44+
var schematic = acc.getLast();
45+
if("#".equals(ch)) {
46+
schematic.positions().add(new Position(x, y));
47+
}
48+
return acc;
49+
});
50+
51+
var keys = schematics.stream()
52+
.filter(s ->
53+
s.positions().stream().anyMatch(p -> p.y() == 6)
54+
)
55+
.toList();
56+
57+
var locks = schematics.stream()
58+
.filter(s ->
59+
s.positions().stream().anyMatch(p -> p.y() == 0)
60+
)
61+
.toList();
62+
63+
// note that we calculate height differently than in the problem description
64+
65+
// all valid lock and key combos
66+
var combos = locks.stream()
67+
.flatMap(lock -> {
68+
var lockHeights = lock.getHeights();
69+
return keys.stream()
70+
.filter(key -> {
71+
var keyHeights = key.getHeights();
72+
return keyHeights.stream()
73+
.map(WithIndex.indexed())
74+
.noneMatch(heightWithIdx -> {
75+
var i = heightWithIdx.index();
76+
var height = heightWithIdx.value();
77+
return lockHeights.get(i) + height > 7;
78+
});
79+
})
80+
.map(key -> new LockAndKey(lock, key));
81+
})
82+
.toList();
83+
84+
return String.valueOf(combos.size());
85+
}
86+
87+
@Override
88+
public String solve() {
89+
Assert.assertEquals("3", solve(getSampleInput()));
90+
return solve(getInput());
91+
}
92+
93+
public static void main(String[] args) {
94+
new Part01().run();
95+
}
96+
97+
}

‎src/main/resources/day25/input

21 KB
Binary file not shown.

‎src/main/resources/day25/sample

236 Bytes
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /