-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC2021_11.java
118 lines (101 loc) Β· 3.35 KB
/
AoC2021_11.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
import static java.util.stream.Collectors.joining;
import java.util.Arrays;
import java.util.List;
import com.github.pareronia.aoc.IntGrid;
import com.github.pareronia.aoc.Grid.Cell;
import com.github.pareronia.aoc.MutableInt;
import com.github.pareronia.aocd.Aocd;
import com.github.pareronia.aocd.Puzzle;
public class AoC2021_11 extends AoCBase {
private final IntGrid grid;
private AoC2021_11(final List<String> input, final boolean debug) {
super(debug);
this.grid = IntGrid.from(input);
}
private void printGrid() {
if (!this.debug) {
return;
}
Arrays.stream(this.grid.getValues()).forEach(r ->
log(Arrays.stream(r)
.mapToObj(Integer::valueOf)
.map(String::valueOf)
.collect(joining(" "))));
}
public static final AoC2021_11 create(final List<String> input) {
return new AoC2021_11(input, false);
}
public static final AoC2021_11 createDebug(final List<String> input) {
return new AoC2021_11(input, true);
}
private void flash(final Cell c, final MutableInt flashes) {
this.grid.setValue(c, 0);
flashes.increment();
this.grid.getAllNeighbours(c)
.filter(n -> this.grid.getValue(n) != 0)
.forEach(n -> {
this.grid.increment(n);
if (this.grid.getValue(n) > 9) {
flash(n, flashes);
}
});
}
private int cycle() {
this.grid.getCells().forEach(this.grid::increment);
final MutableInt flashes = new MutableInt(0);
this.grid.getCells()
.filter(c -> this.grid.getValue(c) > 9)
.forEach(c -> flash(c, flashes));
return flashes.intValue();
}
@Override
public Integer solvePart1() {
int cycle = 0;
int flashes = 0;
log("(" + cycle + ")");
printGrid();
for (cycle = 1; cycle <= 100; cycle++) {
flashes += cycle();
log("(" + cycle + ")");
printGrid();
log("flashes: " + flashes);
}
return flashes;
}
@Override
public Integer solvePart2() {
int cycle = 1;
while (true) {
final int flashes = cycle();
if (flashes == this.grid.size()) {
log("(" + cycle + ")");
printGrid();
log("flashes: " + flashes);
break;
}
cycle++;
}
return cycle;
}
public static void main(final String[] args) throws Exception {
assert AoC2021_11.create(TEST).solvePart1() == 1656;
assert AoC2021_11.create(TEST).solvePart2() == 195;
final Puzzle puzzle = Aocd.puzzle(2021, 11);
puzzle.check(
() -> lap("Part 1", () -> AoC2021_11.create(puzzle.getInputData()).solvePart1()),
() -> lap("Part 2", () -> AoC2021_11.create(puzzle.getInputData()).solvePart2())
);
}
private static final List<String> TEST = splitLines(
"5483143223\r\n" +
"2745854711\r\n" +
"5264556173\r\n" +
"6141336146\r\n" +
"6357385478\r\n" +
"4167524645\r\n" +
"2176841721\r\n" +
"6882881134\r\n" +
"4846848554\r\n" +
"5283751526"
);
}