-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.java
82 lines (71 loc) · 2.15 KB
/
Day14.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
import com.horstmann.adventofcode.*;
int WIDTH;
int HEIGHT;
record Robot(int sx, int sy, int vx, int vy) {}
Location move(Robot r, long t) {
return new Location(Math.floorMod(r.sy + t * r.vy, HEIGHT), Math.floorMod(r.sx + t * r.vx, WIDTH));
}
List<Robot> robots;
int quadrant(Location l) {
int x = l.col();
int y = l.row();
if (y < HEIGHT / 2) {
if (x < WIDTH / 2)
return 1;
else if (x > WIDTH / 2)
return 2;
} else if (y > HEIGHT / 2) {
if (x < WIDTH / 2)
return 3;
else if (x > WIDTH / 2)
return 4;
}
return 0;
}
void parse(Path path) throws IOException {
robots = new ArrayList<>();
var lines = Files.readAllLines(path);
for (var line : lines) {
var ns = Util.parseIntegers(line);
robots.add(new Robot(ns.get(0), ns.get(1), ns.get(2), ns.get(3)));
}
}
Object part1() {
var cs = robots.stream().map(r -> move(r, 100)).map(this::quadrant).collect(Collectors.groupingBy(s -> s, Collectors.counting()));
return cs.getOrDefault(1, 0L) * cs.getOrDefault(2, 0L) * cs.getOrDefault(3, 0L) * cs.getOrDefault(4, 0L);
}
boolean hasClump(CharGrid g, int cutoff) {
var locs = g.findAll('*').toList();
var components = Graphs.connectedComponents(locs, g::sameNeighbors);
var largestClumpSize = components.stream().mapToInt(c -> c.size()).max().getAsInt();
return largestClumpSize >= cutoff;
}
Object part2() {
for (int t = 0; t < Numbers.lcm(HEIGHT, WIDTH); t++) {
var grid = new CharGrid(HEIGHT, WIDTH, ' ');
for (var r : robots) {
var p = move(r, t);
grid.put(p, '*');
}
if (hasClump(grid, 20)) {
IO.println(grid);
return t;
}
}
return null;
}
void main() throws IOException {
Util.time(() -> {
parse(Util.inputPath("a"));
WIDTH = 11;
HEIGHT = 7;
IO.println(part1());
IO.println(part2());
Util.logging = false;
parse(Util.inputPath("z"));
WIDTH = 101;
HEIGHT = 103;
IO.println(part1());
IO.println(part2());
});
}