|
| 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 | +} |
0 commit comments