-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2021_12.java
185 lines (162 loc) Β· 5.91 KB
/
AoC2021_12.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import com.github.pareronia.aoc.MutableInt;
import com.github.pareronia.aoc.StringUtils;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;
public class AoC2021_12
extends SolutionBase<AoC2021_12.System, Integer, Integer> {
private AoC2021_12(final boolean debug) {
super(debug);
}
public static final AoC2021_12 create() {
return new AoC2021_12(false);
}
public static final AoC2021_12 createDebug() {
return new AoC2021_12(true);
}
@Override
protected System parseInput(final List<String> inputs) {
return System.fromInput(inputs);
}
private void dfs(final System system, final Cave start, final Cave end,
final State state,
final BiFunction<Cave, State, Boolean> proceed,
final Runnable onPath) {
if (start.equals(end)) {
onPath.run();
return;
}
for (final Cave to : system.tunnels().get(start)) {
if (proceed.apply(to, state)) {
final State newState = State.copyOf(state);
if (to.isSmall()) {
if (newState.smallCavesSeen.contains(to)) {
newState.smallCavesSeenTwice.add(to);
}
newState.smallCavesSeen.add(to);
}
dfs(system, to, end, newState, proceed, onPath);
}
}
}
private int solve(final System system, final BiFunction<Cave, State, Boolean> proceed) {
final Cave start = system.start();
final State state = new State(new HashSet<>(Set.of(start)), new HashSet<>());
final MutableInt count = new MutableInt();
dfs(system, start, system.end(), state, proceed, () -> count.increment());
return count.intValue();
}
@Override
public Integer solvePart1(final System system) {
return solve(system, (to, state)
-> !to.isSmall()
|| !state.smallCavesSeen.contains(to));
}
@Override
public Integer solvePart2(final System system) {
return solve(system, (to, state)
-> !to.isSmall()
|| !state.smallCavesSeen.contains(to)
|| (!to.isStart()
&& !to.isEnd()
&& state.smallCavesSeenTwice.isEmpty()));
}
@Samples({
@Sample(method = "part1", input = TEST1, debug = false, expected = "10"),
@Sample(method = "part1", input = TEST2, debug = false, expected = "19"),
@Sample(method = "part1", input = TEST3, debug = false, expected = "226"),
@Sample(method = "part2", input = TEST1, debug = false, expected = "36"),
@Sample(method = "part2", input = TEST2, debug = false, expected = "103"),
@Sample(method = "part2", input = TEST3, debug = false, expected = "3509"),
})
public static void main(final String[] args) throws Exception {
AoC2021_12.create().run();
}
private static final String TEST1 = """
start-A\r
start-b\r
A-c\r
A-b\r
b-d\r
A-end\r
b-end
""";
private static final String TEST2 = """
dc-end\r
HN-start\r
start-kj\r
dc-start\r
dc-HN\r
LN-dc\r
HN-end\r
kj-sa\r
kj-HN\r
kj-dc
""";
private static final String TEST3 = """
fs-end\r
he-DX\r
fs-he\r
start-DX\r
pj-DX\r
end-zg\r
zg-sl\r
zg-pj\r
pj-he\r
RW-he\r
fs-DX\r
pj-RW\r
zg-RW\r
start-pj\r
he-WI\r
zg-he\r
pj-fs\r
start-RW
""";
record State(Set<Cave> smallCavesSeen, Set<Cave> smallCavesSeenTwice) {
public static State copyOf(final State state) {
return new State(new HashSet<>(Set.copyOf(state.smallCavesSeen)),
new HashSet<>(Set.copyOf(state.smallCavesSeenTwice)));
}
}
record Cave(String name) {
public boolean isSmall() {
return StringUtils.isAllLowerCase(name);
}
public boolean isStart() {
return this.name.equals("start");
}
public boolean isEnd() {
return this.name.equals("end");
}
}
record Tunnel(Cave from, Cave to) {}
record System(Map<Cave, Set<Cave>> tunnels, Cave start, Cave end) {
public static System fromInput(final List<String> inputs) {
final Map<Cave, Set<Cave>> tunnels = new HashMap<>();
Cave start = null, end = null;
for (final String string : inputs) {
final String[] split = string.split("-");
final Cave from = new Cave(split[0]);
final Cave to = new Cave(split[1]);
tunnels.computeIfAbsent(from, c -> new HashSet<>()).add(to);
tunnels.computeIfAbsent(to, c -> new HashSet<>()).add(from);
for (final Cave cave : Set.of(from, to)) {
if (cave.isStart()) {
start = cave;
}
if (cave.isEnd()) {
end = cave;
}
}
}
return new System(Collections.unmodifiableMap(tunnels), start, end);
}}
}