-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle19.java
85 lines (77 loc) · 2.49 KB
/
Puzzle19.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
package advent2024;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.Math.addExact;
import com.google.common.base.Splitter;
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.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* @author Éamonn McManus
*/
public class Puzzle19 {
private static final String SAMPLE =
"""
r, wr, b, g, bwu, rb, gb, br
brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb
""";
private static final Map<String, Callable<Reader>> INPUT_PRODUCERS =
ImmutableMap.of(
"sample",
() -> new StringReader(SAMPLE),
"problem",
() -> new InputStreamReader(Puzzle19.class.getResourceAsStream("puzzle19.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);
checkArgument(lines.get(1).isEmpty());
List<String> patterns = Splitter.on(", ").splitToList(lines.get(0));
List<String> strings = lines.subList(2, lines.size());
int possibleCount = 0;
long combinationCount = 0;
for (String string : strings) {
long count = possible(patterns, string, new LinkedHashMap<>());
if (count > 0) {
possibleCount++;
combinationCount = addExact(combinationCount, count);
}
}
System.out.printf(
"For %s, possible count %d, combinations %d\n", name, possibleCount, combinationCount);
}
}
}
// I wasted a lot of time making a Trie implementation and tackling a subtle bug in the recursion
// with it, before giving up and using this less efficient approach. It still runs in less than a
// second.
private static long possible(List<String> patterns, String s, Map<String, Long> cache) {
if (s.isEmpty()) {
return 1;
}
long count = 0;
for (String pat : patterns) {
if (s.startsWith(pat)) {
String rest = s.substring(pat.length());
if (!cache.containsKey(rest)) {
cache.put(rest, possible(patterns, rest, cache));
}
count += cache.get(rest);
}
}
return count;
}
}