Skip to content

Commit 05748d3

Browse files
committed
Day 11: Flashing octopi - detonating neighbours' flashes
1 parent 78f06ad commit 05748d3

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Day11;
2+
3+
import advent.utils.AdventFileUtils;
4+
5+
import java.util.List;
6+
7+
public class Day11Main {
8+
9+
public static void main(String[] args) {
10+
List<List<Integer>> inputAsMatrix = getInput();
11+
inputAsMatrix.forEach(System.out::println);
12+
13+
OctopusDetonator octopusDetonator = new OctopusDetonator();
14+
long octopusFlashes = octopusDetonator.detonateOctopusFlashes(inputAsMatrix);
15+
System.out.println(octopusFlashes);
16+
17+
long allFlashingStep = octopusDetonator.findAllFlashingStep(inputAsMatrix);
18+
System.out.println(allFlashingStep);
19+
}
20+
21+
protected static List<List<Integer>> getInput() {
22+
return AdventFileUtils.readClassInputIntoIntegerMatrix(Day11Main.class);
23+
}
24+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package Day11;
2+
3+
import java.awt.*;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
public class OctopusDetonator {
9+
10+
private final int GRID_MAX = 10;
11+
12+
public long detonateOctopusFlashes(List<List<Integer>> octopuses) {
13+
int step = 0;
14+
long flashCount = 0;
15+
while (step != 100) {
16+
incrementEnergy(octopuses);
17+
18+
Set<Point> flashed = getFlashedOctopuses(octopuses);
19+
flashed.forEach(flash -> octopuses.get(flash.y).set(flash.x, 0));
20+
flashCount += flashed.size();
21+
step++;
22+
}
23+
return flashCount;
24+
}
25+
26+
public long findAllFlashingStep(List<List<Integer>> octopuses) {
27+
int step = 0;
28+
long flashCount = 0;
29+
while (flashCount != 100) {
30+
incrementEnergy(octopuses);
31+
Set<Point> flashed = getFlashedOctopuses(octopuses);
32+
flashed.forEach(flash -> octopuses.get(flash.y).set(flash.x, 0));
33+
flashCount = flashed.size();
34+
step++;
35+
}
36+
return step;
37+
}
38+
39+
private Set<Point> getFlashedOctopuses(List<List<Integer>> octopuses) {
40+
Set<Point> flashed = new HashSet<>();
41+
for (int row = 0; row < GRID_MAX; row++) {
42+
for (int column = 0; column < GRID_MAX; column++) {
43+
Integer octopusEnergy = octopuses.get(row).get(column);
44+
if (octopusEnergy > 9) {
45+
flash(octopuses, new Point(column, row), flashed);
46+
}
47+
}
48+
}
49+
return flashed;
50+
}
51+
52+
private void incrementEnergy(List<List<Integer>> octopuses) {
53+
for (int row = 0; row < GRID_MAX; row++) {
54+
for (int column = 0; column < GRID_MAX; column++) {
55+
octopuses.get(row).set(column, octopuses.get(row).get(column) + 1);
56+
}
57+
}
58+
}
59+
60+
private void flash(List<List<Integer>> octopuses, Point flash, Set<Point> flashed) {
61+
if (flashed.contains(flash)) {
62+
return;
63+
}
64+
flashed.add(flash);
65+
66+
Set<Point> neighbours = getNeighbours(flash);
67+
for (Point neighbour : neighbours) {
68+
Integer neighbourEnergy = octopuses.get(neighbour.y).get(neighbour.x);
69+
neighbourEnergy++;
70+
octopuses.get(neighbour.y).set(neighbour.x, neighbourEnergy);
71+
if (neighbourEnergy > 9) {
72+
flash(octopuses, neighbour, flashed);
73+
}
74+
}
75+
}
76+
77+
private Set<Point> getNeighbours(Point flash) {
78+
Set<Point> neighbours = new HashSet<>();
79+
if (flash.x > 0) {
80+
// left neighbour
81+
neighbours.add(new Point(flash.x - 1, flash.y));
82+
if (flash.y > 0) {
83+
// left Neighbour Above
84+
neighbours.add(new Point(flash.x - 1, flash.y - 1));
85+
}
86+
if (flash.y < GRID_MAX - 1) {
87+
//left Neighbour Below
88+
neighbours.add(new Point(flash.x - 1, flash.y + 1));
89+
}
90+
}
91+
if (flash.x < GRID_MAX - 1) {
92+
// right Neighbour
93+
neighbours.add(new Point(flash.x + 1, flash.y));
94+
if (flash.y > 0) {
95+
// right Neighbour Above
96+
neighbours.add(new Point(flash.x + 1, flash.y - 1));
97+
}
98+
if (flash.y < GRID_MAX - 1) {
99+
// right Neighbour Below
100+
neighbours.add(new Point(flash.x + 1, flash.y + 1));
101+
}
102+
}
103+
if (flash.y > 0) {
104+
// above Neighbour
105+
neighbours.add(new Point(flash.x, flash.y - 1));
106+
}
107+
if (flash.y < GRID_MAX - 1) {
108+
// below Neighbour
109+
neighbours.add(new Point(flash.x, flash.y + 1));
110+
}
111+
return neighbours;
112+
}
113+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
4738615556
2+
6744423741
3+
2812868827
4+
8844365624
5+
4546674266
6+
4518674278
7+
7457237431
8+
4524873247
9+
3153341314
10+
3721414667

0 commit comments

Comments
 (0)