-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day17Part1.java
85 lines (69 loc) · 2.94 KB
/
Day17Part1.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
package uk.oczadly.karl.aoc20.solution.day17;
import uk.oczadly.karl.aoc20.PuzzleSolution;
import uk.oczadly.karl.aoc20.input.PuzzleInput;
import uk.oczadly.karl.aoc20.util.Grid2D;
import uk.oczadly.karl.aoc20.util.Grid3D;
/**
* @author Karl Oczadly
*/
public class Day17Part1 extends PuzzleSolution {
public Day17Part1() { super(17, 1); } // Initializes the day and part number
@Override
public Object solve(PuzzleInput input) {
// Parse input plane, and convert to 3D grid
Grid2D<Boolean> initPlane = input.asGrid(c -> c == '#');
Grid3D<Boolean> grid = new Grid3D<>(initPlane.getWidth(), initPlane.getHeight(), 1, false);
grid.setPlaneZ(0, 0, 0, initPlane);
// Run 6 iterations
ConwayCube cubes = new ConwayCube(grid);
for (int i = 0; i < 6; i++)
cubes.nextIteration();
// Count active cells
return cubes.grid.streamElements()
.filter(Boolean::valueOf) // True = active
.count();
}
static class ConwayCube {
private Grid3D<Boolean> grid;
public ConwayCube(Grid3D<Boolean> grid) {
this.grid = grid;
}
/** Returns the current cell state for the given next coordinate. Each iteration increases the size of the
* grid by 2, acting as a padding around the input. */
private boolean getStateFor(int x, int y, int z) {
return grid.getOutOfBounds(x - 1, y - 1, z - 1, false);
}
public void nextIteration() {
// Create the next grid container
Grid3D<Boolean> nextGrid = new Grid3D<>(grid.getLengthX() + 2, grid.getLengthY() + 2,
grid.getLengthZ() + 2, false);
// Process mutations
for (int z = 0; z < nextGrid.getLengthZ(); z++)
for (int x = 0; x < nextGrid.getLengthX(); x++)
for (int y = 0; y < nextGrid.getLengthY(); y++)
nextGrid.set(x, y, z, nextIteration(x, y, z));
// Apply changes
grid = nextGrid;
}
public boolean nextIteration(int x, int y, int z) {
boolean active = getStateFor(x, y, z); // Current state
// Count neighbours
int neighbours = 0;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
for (int dz = -1; dz <= 1; dz++) {
if (dx == 0 && dy == 0 && dz == 0) continue; // Ignore self, not a neighbour
if (getStateFor(x + dx, y + dy, z + dz))
neighbours++;
}
}
}
// Mutate
if (active) {
return neighbours >= 2 && neighbours <= 3;
} else {
return neighbours == 3;
}
}
}
}