-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle25.java
139 lines (127 loc) · 3.6 KB
/
Puzzle25.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package advent2024;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.IntStream;
/**
* @author Éamonn McManus
*/
public class Puzzle25 {
private static final String SAMPLE =
"""
#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####
""";
private static final Map<String, Callable<Reader>> INPUT_PRODUCERS =
ImmutableMap.of(
"sample",
() -> new StringReader(SAMPLE),
"problem",
() -> new InputStreamReader(Puzzle25.class.getResourceAsStream("puzzle25.txt")));
public static void main(String[] args) throws Exception {
for (var entry : INPUT_PRODUCERS.entrySet()) {
String name = entry.getKey();
try (Reader r = entry.getValue().call()) {
List<String> lines = CharStreams.readLines(r);
ImmutableList.Builder<Lock> locksBuilder = ImmutableList.builder();
ImmutableList.Builder<Key> keysBuilder = ImmutableList.builder();
for (int i = 0; i < lines.size(); i += 8) {
checkArgument(i == 0 || lines.get(i - 1).isEmpty());
List<String> sublines = lines.subList(i, i + 7);
if (sublines.getFirst().equals("#####")) {
locksBuilder.add(Lock.parse(sublines));
} else if (sublines.getLast().equals("#####")) {
keysBuilder.add(Key.parse(sublines));
} else {
throw new AssertionError(sublines.toString());
}
}
int count = 0;
for (Lock lock : locksBuilder.build()) {
for (Key key : keysBuilder.build()) {
if (key.fits(lock)) {
count++;
}
}
}
System.out.printf("For %s, count is %d\n", name, count);
}
}
}
private record Lock(ImmutableList<Integer> heights) {
static Lock parse(List<String> lines) {
ImmutableList.Builder<Integer> heightsBuilder = ImmutableList.builder();
for (int col = 0; col < 5; col++) {
for (int row = 1; row < 7; row++) {
if (lines.get(row).charAt(col) == '.') {
heightsBuilder.add(row - 1);
break;
}
}
}
var heights = heightsBuilder.build();
checkState(heights.size() == 5);
return new Lock(heights);
}
}
private record Key(ImmutableList<Integer> heights) {
boolean fits(Lock lock) {
return IntStream.range(0, 5).allMatch(i -> lock.heights.get(i) + this.heights.get(i) < 6);
}
static Key parse(List<String> lines) {
ImmutableList.Builder<Integer> heightsBuilder = ImmutableList.builder();
for (int col = 0; col < 5; col++) {
for (int row = 5; row >= 0; row--) {
if (lines.get(row).charAt(col) == '.') {
heightsBuilder.add(5 - row);
break;
}
}
}
var heights = heightsBuilder.build();
checkState(heights.size() == 5);
return new Key(heights);
}
}
}