-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOctopusFlashNavigator.java
More file actions
127 lines (115 loc) · 4.29 KB
/
OctopusFlashNavigator.java
File metadata and controls
127 lines (115 loc) · 4.29 KB
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
119
120
121
122
123
124
125
126
127
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
/**
* Solution to Day 11: Dumbo Octopus.
*
* @author bluebillxp
*/
public class OctopusFlashNavigator {
private static class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
List<String> input = AdventHelper.readInput("input-day11-dumbo-octopus.txt");
System.out.println("Day 11: Dumbo Octopus. " + input.size() + " lines loaded.");
int[][] map = loadMap(input);
System.out.println("Day 11: Dumbo Octopus. --- Part One ---");
System.out.println("How many total flashes are there after 100 steps?");
final int answerOne = solutionPartOne(map);
System.out.println("Answer: " + answerOne);
System.out.println("\nDay 11: Dumbo Octopus. --- Part Two ---");
System.out.println("What is the first step during which all octopuses flash?");
final int answerTwo = solutionPartTwo(map);
System.out.println("Answer: " + answerTwo);
}
/**
* Calculates how many total flashes are there after 100 steps.
*
* @param map Defined input from the challenge.
*
* @return answer
*/
private static int solutionPartOne(int[][] map) {
int totalFlashes = 0;
for (int i = 0; i < 100; i++) {
totalFlashes += flash(map, null, new HashSet<>());
}
return totalFlashes;
}
/**
* Calculates the first step during which all octopuses flash.
*
* @param map Defined input from the challenge.
*
* @return answer
*/
private static int solutionPartTwo(int[][] map) {
int steps = 100; // Continued from part 1.
int allFlashes = map.length * map.length;
do {
steps++;
} while (flash(map, null, new HashSet<>()) != allFlashes);
return steps;
}
private static int flash(int[][] map, Queue<Point> flashPoints, Set<String> flashedPoints) {
Queue<Point> newFlashPoints = new LinkedList<>();
if (flashPoints == null) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
chargeFlash(i, j, map, newFlashPoints, flashedPoints);
}
}
} else {
for (Point point : flashPoints) {
chargeFlash(point.x - 1, point.y - 1, map, newFlashPoints, flashedPoints);
chargeFlash(point.x, point.y - 1, map, newFlashPoints, flashedPoints);
chargeFlash(point.x + 1, point.y - 1, map, newFlashPoints, flashedPoints);
chargeFlash(point.x - 1, point.y, map, newFlashPoints, flashedPoints);
chargeFlash(point.x + 1, point.y, map, newFlashPoints, flashedPoints);
chargeFlash(point.x - 1, point.y + 1, map, newFlashPoints, flashedPoints);
chargeFlash(point.x, point.y + 1, map, newFlashPoints, flashedPoints);
chargeFlash(point.x + 1, point.y + 1, map, newFlashPoints, flashedPoints);
}
}
if (newFlashPoints.isEmpty()) {
return 0;
}
return newFlashPoints.size() + flash(map, newFlashPoints, flashedPoints);
}
private static void chargeFlash(
int x, int y, int[][] map, Queue<Point> flashPoints, Set<String> flashedPoints) {
if (x < 0 || x == map.length || y < 0 || y == map.length) {
return;
}
String flashTag = x + "-" + y;
if (flashedPoints.contains(flashTag)) {
return;
}
map[x][y]++;
if (map[x][y] % 10 == 0) {
map[x][y] = 0;
flashPoints.add(new Point(x, y));
flashedPoints.add(flashTag);
}
}
private static int[][] loadMap(List<String> input) {
int[][] map = new int[input.size()][input.size()];
for (int i = 0; i < input.size(); i++) {
String line = input.get(i);
for (int j = 0; j < line.length(); j++) {
map[i][j] = (line.charAt(j) - '0');
System.out.print(map[i][j] + " ");
}
System.out.print("\n");
}
return map;
}
}