-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle22.java
102 lines (95 loc) · 3.11 KB
/
Puzzle22.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
package advent2024;
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.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* @author Éamonn McManus
*/
public class Puzzle22 {
private static final String SAMPLE1 =
"""
1
10
100
2024
""";
private static final String SAMPLE2 =
"""
1
2
3
2024
""";
// Took me embarrassingly long to realize that the sample from Part 1 was not the same in Part 2.
private static final Map<String, Callable<Reader>> INPUT_PRODUCERS =
ImmutableMap.of(
"sample 1",
() -> new StringReader(SAMPLE1),
"sample 2",
() -> new StringReader(SAMPLE2),
"problem",
() -> new InputStreamReader(Puzzle22.class.getResourceAsStream("puzzle22.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);
List<Long> secrets = lines.stream().map(Long::valueOf).toList();
long sum = secrets.stream().map(s -> nthNextSecret(2000, s)).reduce(0L, Math::addExact);
System.out.printf("For %s, sum of 2000th secret numbers is %d\n", name, sum);
System.out.printf("For %s, max bananas is %d\n", name, part2(secrets));
}
}
}
private static long part2(List<Long> secrets) {
// Map from each sequence of 4 differences to the total prices for the first occurrence of that
// sequence in the secret sequence for each starting number.
Map<ImmutableList<Integer>, Long> totals = new LinkedHashMap<>();
for (long s : secrets) {
Map<ImmutableList<Integer>, Integer> results = new LinkedHashMap<>();
List<Integer> window = new ArrayList<>();
int lastPrice = 0;
for (int i = 0; i < 2000; i++) {
int price = (int) s % 10;
if (i > 0) {
int diff = price - lastPrice;
window.add(diff);
if (window.size() > 4) {
window.removeFirst();
}
if (window.size() == 4) {
// Have to make a copy of the list before storing it as a key in the map.
results.putIfAbsent(ImmutableList.copyOf(window), price);
}
}
lastPrice = price;
s = nextSecret(s);
}
results.forEach(
(diffList, price) -> totals.put(diffList, totals.getOrDefault(diffList, 0L) + price));
}
return Collections.max(totals.values());
}
private static long nthNextSecret(int n, long s) {
for (int i = 0; i < n; i++) {
s = nextSecret(s);
}
return s;
}
private static long nextSecret(long s) {
long mask = (1 << 24) - 1;
s = ((s << 6) ^ s) & mask;
s = (s >> 5) ^ s;
s = ((s << 11) ^ s) & mask;
return s;
}
}