-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle2.java
84 lines (77 loc) · 2.71 KB
/
Puzzle2.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
package advent2024;
import static java.lang.Integer.signum;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* @author Éamonn McManus
*/
public class Puzzle2 {
private static final String SAMPLE =
"""
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
""";
private static final Map<String, Callable<Reader>> INPUT_PRODUCERS =
ImmutableMap.of(
"sample", () -> new StringReader(SAMPLE),
"problem", () -> new InputStreamReader(Puzzle2.class.getResourceAsStream("puzzle2.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<List<Integer>> readings =
lines.stream()
.map(line -> Splitter.on(" ").splitToStream(line).map(Integer::valueOf).toList())
.toList();
long count = readings.stream().filter(Puzzle2::safe).count();
System.out.println("Count for " + name + " is " + count);
long countWithDampener = readings.stream().filter(Puzzle2::safeWithDampener).count();
System.out.println("Count with dampener for " + name + " is " + countWithDampener);
}
}
}
private static boolean safe(List<Integer> reading) {
int prevDelta = 0;
for (int i = 1; i < reading.size(); i++) {
int delta = reading.get(i) - reading.get(i - 1);
if (prevDelta != 0 && signum(delta) != signum(prevDelta)) {
return false;
}
if (delta == 0 || Math.abs(delta) > 3) {
return false;
}
prevDelta = delta;
}
return true;
}
private static boolean safeWithDampener(List<Integer> reading) {
if (safe(reading)) {
return true;
}
// Try removing each number to see if the remaining list is safe. This is inefficient, and I
// originally modified `safe` to return the index of the first failing number, if any. But that
// proved fiddly to get right. The lists in the problem input are short enough to make this
// brute-force approach essentially immediate.
for (int i = 0; i < reading.size(); i++) {
List<Integer> withRemoval = new ArrayList<>(reading.subList(0, i));
withRemoval.addAll(reading.subList(i + 1, reading.size()));
if (safe(withRemoval)) {
return true;
}
}
return false;
}
}