-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC2016_22.java
330 lines (297 loc) Β· 12.8 KB
/
AoC2016_22.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import static com.github.pareronia.aoc.AssertUtils.assertFalse;
import static com.github.pareronia.aoc.AssertUtils.assertTrue;
import static com.github.pareronia.aoc.StringOps.splitLines;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.summingInt;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import com.github.pareronia.aoc.geometry.Direction;
import com.github.pareronia.aoc.geometry.Point;
import com.github.pareronia.aoc.geometry.Position;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;
public class AoC2016_22
extends SolutionBase<AoC2016_22.Cluster, Integer, Integer> {
private AoC2016_22(final boolean debug) {
super(debug);
}
public static AoC2016_22 create() {
return new AoC2016_22(false);
}
public static AoC2016_22 createDebug() {
return new AoC2016_22(true);
}
@Override
protected Cluster parseInput(final List<String> inputs) {
return new Cluster(inputs.stream()
.skip(2)
.map(Node::fromInput)
.collect(toList()));
}
@Override
public Integer solvePart1(final Cluster cluster) {
return (int) cluster.nodes.stream()
.filter(Node::isNotEmpty)
.flatMap(a -> cluster.nodes.stream()
.filter(b -> !a.equals(b))
.filter(b -> a.used() <= b.available()))
.count();
}
private void visualize(final Cluster cluster) {
final Integer maxX = cluster.getMaxX();
final Integer maxY = cluster.getMaxY();
final List<Node> sorted = cluster.nodes.stream()
.sorted(comparing(n -> n.x() * maxY + n.y()))
.collect(toList());
final List<List<Node>> grid = Stream.iterate(0, i -> i <= maxX, i -> i + 1)
.map(i -> sorted.stream()
.skip(i * (maxY + 1))
.takeWhile(n -> n.x() == i)
.collect(toList()))
.collect(toList());
final Set<Node> unusableNodes = cluster.getUnusableNodes();
final Node emptyNode = cluster.getEmptyNode();
final Node goalNode = cluster.getGoalNode();
final Node accessibleNode = cluster.getAccessibleNode();
for (final List<Node> row : grid) {
final String line = row.stream()
.map(n -> {
if (unusableNodes.contains(n)) {
return " # ";
} else if (emptyNode.equals(n)) {
return " _ ";
} else if (goalNode.equals(n)) {
return " G ";
} else if (accessibleNode.equals(n)) {
return "(.)";
} else {
return " . ";
}
})
.collect(joining(""));
log(line);
}
}
private Position toPosition(final Node node) {
return Position.of(node.x(), node.y());
}
private Function<Path, Boolean> stopAt(
final Position position, final List<Path> paths) {
return path -> {
if (path.isAt(position)) {
paths.add(path);
return true;
}
return false;
};
}
private Integer solve2(final Cluster cluster) {
visualize(cluster);
final Set<Position> unusableNodes = cluster.getUnusableNodes().stream()
.map(this::toPosition)
.collect(toSet());
final Position emptyNode = toPosition(cluster.getEmptyNode());
final Position accessibleNode = toPosition(cluster.getAccessibleNode());
final Integer maxX = cluster.getMaxX();
final Integer maxY = cluster.getMaxY();
final Position goalNode = toPosition(cluster.getGoalNode());
log("Unusable: "+ unusableNodes);
log("Empty: " + emptyNode);
log("Accessible: " + accessibleNode);
log("Goal: " + goalNode);
final List<Path> paths = new ArrayList<>();
final Position destination1
= Position.of(goalNode.getX() - 1, goalNode.getY());
assert !unusableNodes.contains(destination1);
final Position max = Position.of(maxX, maxY);
new PathFinder(emptyNode, destination1, max, unusableNodes)
.findPaths(stopAt(destination1, paths));
final Position destination2
= Position.of(accessibleNode.getX() + 1, accessibleNode.getY());
assert !unusableNodes.contains(destination2);
new PathFinder(goalNode, destination2, max, unusableNodes)
.findPaths(stopAt(destination2, paths));
unusableNodes.add(destination2);
new PathFinder(goalNode, accessibleNode, max, unusableNodes)
.findPaths(stopAt(accessibleNode, paths));
final Integer length = paths.stream()
.map(Path::length)
.collect(summingInt(Integer::valueOf));
log(length);
return length + 1;
}
private Integer solve2Cheat(final Cluster cluster) {
visualize(cluster);
final Set<Node> unusableNodes = cluster.getUnusableNodes();
log(unusableNodes);
final Set<Integer> holeYs = unusableNodes.stream()
.map(Node::y)
.collect(toSet());
assertTrue(holeYs.size() == 1, () -> "Expected all unusable nodes in 1 row");
final Integer holeY = holeYs.iterator().next();
if (holeY <= 1) {
throw new IllegalStateException("Unsolvable");
}
assertFalse(unusableNodes.stream()
.max(comparing(Node::x))
.map(Node::x)
.orElseThrow() != cluster.getMaxX(),
() -> "Expected unusable row to touch side");
final Integer holeX = unusableNodes.stream()
.min(comparing(Node::x))
.map(Node::x)
.orElseThrow();
final Position hole = Position.of(holeX - 1, holeY);
final Position emptyNode = toPosition(cluster.getEmptyNode());
final int part1 = emptyNode.manhattanDistance(hole);
final Position goalNode = toPosition(cluster.getGoalNode());
final int part2 = hole.manhattanDistance(
Position.of(goalNode.getX() - 1, goalNode.getY()));
final int part3 = 5 * (goalNode.getX() - 1);
return part1 + part2 + part3 + 1;
}
@Override
public Integer solvePart2(final Cluster cluster) {
return solve2Cheat(cluster);
}
@Override
@Samples({
@Sample(method = "part1", input = TEST, expected = "7")
})
public void samples() {
final AoC2016_22 test = AoC2016_22.createDebug();
assert test.solve2(test.parseInput(splitLines(TEST))) == 7;
}
public static void main(final String[] args) throws Exception {
AoC2016_22.createDebug().run();
}
private static final String TEST = """
root@ebhq-gridcenter# df -h\r
Filesystem Size Used Avail Use%\r
/dev/grid/node-x0-y0 10T 8T 2T 80%\r
/dev/grid/node-x0-y1 11T 6T 5T 54%\r
/dev/grid/node-x0-y2 32T 28T 4T 87%\r
/dev/grid/node-x1-y0 9T 7T 2T 77%\r
/dev/grid/node-x1-y1 8T 0T 8T 0%\r
/dev/grid/node-x1-y2 11T 7T 4T 63%\r
/dev/grid/node-x2-y0 10T 6T 4T 60%\r
/dev/grid/node-x2-y1 9T 8T 1T 88%\r
/dev/grid/node-x2-y2 9T 6T 3T 66%
""";
private static final class PathFinder {
private final Position start;
private final Position destination;
private final Position max;
private final Set<Position> unusable;
public PathFinder(
final Position start, final Position destination,
final Position max, final Set<Position> unusable
) {
this.start = start;
this.destination = destination;
this.max = max;
this.unusable = unusable;
}
public void findPaths(final Function<Path, Boolean> stop) {
final Deque<Path> paths = new ArrayDeque<>();
Path path = new Path(0, this.start);
paths.add(path);
final Set<Position> seen = new HashSet<>();
while (!stop.apply(path) && !paths.isEmpty()) {
path = paths.removeFirst();
if (path.isAt(this.destination)) {
continue;
}
for (final Direction direction : Direction.CAPITAL) {
final Path newPath = buildNewPath(path, direction);
if (isInBounds(newPath.position())
&& isUsable(newPath.position())
&& !seen.contains(newPath.position())) {
paths.add(newPath);
seen.add(newPath.position());
}
}
}
}
private Path buildNewPath(final Path path, final Direction direction) {
return new Path(path.length() + 1,
path.position().translate(direction));
}
private boolean isInBounds(final Position position) {
return position.getX() >= 0
&& position.getY() >= 0
&& position.getX() <= this.max.getX()
&& position.getY() <= this.max.getY();
}
private boolean isUsable(final Point position) {
return !this.unusable.contains(position);
}
}
record Path(int length, Position position) {
public boolean isAt(final Position position) {
return this.position.equals(position);
}
}
record Node(int x, int y, int used, int available) {
public static Node fromInput(final String line) {
final String[] splits = line.split("\\s+");
final String[] xy = splits[0].split("-");
return new Node(
Integer.parseInt(xy[1].substring(1)),
Integer.parseInt(xy[2].substring(1)),
Integer.parseInt(splits[2].substring(0, splits[2].length() - 1)),
Integer.parseInt(splits[3].substring(0, splits[3].length() - 1))
);
}
public boolean isNotEmpty() {
return this.used != 0;
}
}
record Cluster(List<Node> nodes) {
public Set<Node> getUnusableNodes() {
final Integer maxAvailable = this.nodes.stream()
.max(comparing(Node::available))
.map(Node::available).orElseThrow();
return this.nodes.stream()
.filter(n -> n.used() > maxAvailable)
.collect(toSet());
}
public Node getEmptyNode() {
final List<Node> emptyNodes = this.nodes.stream()
.filter(n -> n.used() == 0)
.collect(toList());
assertTrue(emptyNodes.size() == 1, () -> "Expected 1 empty node");
return emptyNodes.get(0);
}
public Integer getMaxX() {
return this.nodes.stream()
.max(comparing(Node::x))
.map(Node::x).orElseThrow();
}
public Integer getMaxY() {
return this.nodes.stream()
.max(comparing(Node::y))
.map(Node::y).orElseThrow();
}
public Node getGoalNode() {
return this.nodes.stream()
.filter(n -> n.x() == getMaxX() && n.y() == 0)
.findFirst().orElseThrow();
}
public Node getAccessibleNode() {
return this.nodes.stream()
.filter(n -> n.x() == 0 && n.y() == 0)
.findFirst().orElseThrow();
}
}
}