-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvent12.java
189 lines (166 loc) · 5.89 KB
/
Advent12.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;
public class Advent12 {
public static void main(String[] args) throws IOException {
boolean example = ((args.length > 0) && (args[0].equals("example")));
String file = (example) ? "data/example_12.txt" : "data/12.txt";
String[] data = Shared.readFile(file);
// Part 1:
int[][] grid = new int[data.length][data[0].length()];
int xStart = 0;
int yStart = 0;
int xEnd = 0;
int yEnd = 0;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length(); j++) {
int val = data[i].charAt(j);
if (val == 'S') {
grid[i][j] = 'a';
xStart = i;
yStart = j;
} else if (val == 'E') {
grid[i][j] = 'z';
xEnd = i;
yEnd = j;
} else {
grid[i][j] = data[i].charAt(j);
}
}
}
int solution_a = dijkstra(grid, xStart, yStart, xEnd, yEnd);
System.out.println("Solution 12a: " + solution_a);
if (example) {
int correct_a = 31;
Shared.checkResult(solution_a, correct_a);
}
// Part 2:
int minDistance = Integer.MAX_VALUE;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length(); j++) {
if (data[i].charAt(j) == 'a') {
int distance = dijkstra(grid, i, j, xEnd, yEnd);
if (distance < minDistance) {
minDistance = distance;
}
}
}
}
int solution_b = minDistance;
System.out.println("Solution 12b: " + solution_b);
if (example) {
int correct_b = 29;
Shared.checkResult(solution_b, correct_b);
}
}
private static int dijkstra(int[][] grid, int xStart, int yStart, int xEnd, int yEnd) {
PriorityQueue<Node> queue = new PriorityQueue<>();
HashSet<Node> visited = new HashSet<>();
HashMap<Node, Integer> distances = new HashMap<>();
// add starting node to queue with cost 0
Node startNode = new Node(xStart, yStart, 0);
queue.add(startNode);
distances.put(startNode, 0);
int i = 0;
while (!queue.isEmpty() && i < 10000000) {
i += 1;
// take the first point in the queue, which will be the one with the shortest distance to it
Node currentNode = queue.remove();
if (visited.contains(currentNode)) {
continue;
}
visited.add(currentNode);
int distance = distances.getOrDefault(currentNode, Integer.MAX_VALUE);
// if we've reached the destination, we're done
if (currentNode.x == xEnd && currentNode.y == yEnd) {
break; // distance is the solution
}
// # get the distance to each neighbor
// -- if it's less than we've seen previously, add it to the queue
for (Node nextNode : getNeighbors(currentNode, grid)) {
if (visited.contains(nextNode)) {
continue;
}
int newDistance = distance + 1;
if (newDistance < distances.getOrDefault(nextNode, Integer.MAX_VALUE)) {
distances.put(nextNode, newDistance);
nextNode.cost = newDistance;
queue.add(nextNode);
}
}
}
return distances.getOrDefault(new Node(xEnd, yEnd), Integer.MAX_VALUE);
}
private static List<Node> getNeighbors(Node node, int[][] grid) {
return getNeighbors(node, grid, 1);
}
private static List<Node> getNeighbors(Node node, int[][] grid, int maxStep) {
int xMax = grid.length;
int yMax = grid[0].length;
List<Node> neighbors = new ArrayList<>();
if (node.x < xMax - 1) {
int xNext = node.x + 1;
int yNext = node.y;
if (grid[xNext][yNext] - grid[node.x][node.y] <= maxStep) {
neighbors.add(new Node(xNext, yNext));
}
}
if (node.y < yMax - 1) {
int xNext = node.x;
int yNext = node.y + 1;
if (grid[xNext][yNext] - grid[node.x][node.y] <= maxStep) {
neighbors.add(new Node(xNext, yNext));
}
}
if (node.x > 0) {
int xNext = node.x - 1;
int yNext = node.y;
if (grid[xNext][yNext] - grid[node.x][node.y] <= maxStep) {
neighbors.add(new Node(xNext, yNext));
}
}
if (node.y > 0) {
int xNext = node.x;
int yNext = node.y - 1;
if (grid[xNext][yNext] - grid[node.x][node.y] <= maxStep) {
neighbors.add(new Node(xNext, yNext));
}
}
return neighbors;
}
}
class Node implements Comparable<Node> {
final int x;
final int y;
int cost;
private final int hashCode;
protected Node(int x, int y) {
this.x = x;
this.y = y;
this.cost = 0;
this.hashCode = Objects.hash(x, y);
}
protected Node(int x, int y, int cost) {
this.x = x;
this.y = y;
this.cost = cost;
this.hashCode = Objects.hash(x, y);
}
@Override
public int compareTo(Node o) {
return Integer.compare(this.cost, o.cost);
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Node that = (Node) o;
return x == that.x && y == that.y;
}
@Override
public int hashCode() {
return this.hashCode;
}
}