-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle10.java
84 lines (80 loc) · 2.84 KB
/
Puzzle10.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 adventlib.CharGrid;
import adventlib.CharGrid.Coord;
import adventlib.Dir;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.SetMultimap;
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 Puzzle10 {
private static final String SAMPLE =
"""
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
""";
private static final Map<String, Callable<Reader>> INPUT_PRODUCERS =
ImmutableMap.of(
"sample",
() -> new StringReader(SAMPLE),
"problem",
() -> new InputStreamReader(Puzzle10.class.getResourceAsStream("puzzle10.txt")));
public static void main(String[] args) throws Exception {
// For this one, I started writing a solution for Part 1 that I realized was wrong, but when
// I got to Part 2, I realized that the previously wrong approach was right there.
for (var entry : INPUT_PRODUCERS.entrySet()) {
String name = entry.getKey();
try (Reader r = entry.getValue().call()) {
List<String> lines = CharStreams.readLines(r);
var grid = new CharGrid(lines);
SetMultimap<Character, Coord> digitToCoord = HashMultimap.create();
for (Coord coord : grid.coords()) {
digitToCoord.put(grid.get(coord), coord);
}
SetMultimap<Coord, Coord> reachableNines = HashMultimap.create();
Map<Coord, Long> routeCounts = new LinkedHashMap<>();
for (Coord coord : digitToCoord.get('9')) {
reachableNines.put(coord, coord);
routeCounts.put(coord, 1L);
}
for (char cur = '8'; cur >= '0'; cur = (char) (cur - 1)) {
char next = (char) (cur + 1);
for (Coord curCoord : digitToCoord.get(cur)) {
long routeCount = 0;
for (Dir dir : Dir.NEWS) {
Coord neighbour = dir.move(curCoord, 1);
if (grid.get(neighbour) == next) {
reachableNines.putAll(curCoord, reachableNines.get(neighbour));
routeCount += routeCounts.get(neighbour);
}
}
routeCounts.put(curCoord, routeCount);
}
}
long part1Total = 0;
long part2Total = 0;
for (Coord coord : digitToCoord.get('0')) {
part1Total += reachableNines.get(coord).size();
part2Total += routeCounts.get(coord);
}
System.out.printf("Part 1 total for %s is %d\n", name, part1Total);
System.out.printf("Part 2 total for %s is %d\n", name, part2Total);
}
}
}
}