-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDay22.java
138 lines (116 loc) · 4.12 KB
/
Day22.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
package com.sbaars.adventofcode.year16.days;
import com.sbaars.adventofcode.year16.Day2016;
import java.util.*;
import java.util.stream.Collectors;
public class Day22 extends Day2016 {
public Day22() {
super(22);
}
public static void main(String[] args) {
new Day22().printParts();
}
@Override
public Object part1() {
List<Node> nodes = parseNodes();
int count = 0;
for (int i = 0; i < nodes.size(); i++) {
Node a = nodes.get(i);
if (a.used == 0) continue;
for (int j = 0; j < nodes.size(); j++) {
if (i == j) continue;
Node b = nodes.get(j);
if (a.used <= b.avail) {
count++;
}
}
}
return count;
}
private List<Node> parseNodes() {
String input = day();
return Arrays.stream(input.split("\n"))
.filter(line -> line.startsWith("/dev/grid/node"))
.map(line -> {
String[] parts = line.trim().split("\\s+");
String[] nameParts = parts[0].split("-");
int x = Integer.parseInt(nameParts[1].substring(1));
int y = Integer.parseInt(nameParts[2].substring(1));
int size = Integer.parseInt(parts[1].replace("T", ""));
int used = Integer.parseInt(parts[2].replace("T", ""));
int avail = Integer.parseInt(parts[3].replace("T", ""));
return new Node(x, y, size, used, avail);
})
.collect(Collectors.toList());
}
@Override
public Object part2() {
List<Node> nodes = parseNodes();
Node empty = findEmptyNode(nodes);
if (empty == null) return "No empty node found";
int maxX = findMaxXAtY0(nodes);
int stepsInitial = bfs(empty, maxX - 1, nodes);
return stepsInitial + 1 + (maxX - 1) * 5;
}
private Node findEmptyNode(List<Node> nodes) {
return nodes.stream()
.filter(n -> n.used == 0)
.findFirst()
.orElse(null);
}
private int findMaxXAtY0(List<Node> nodes) {
return nodes.stream()
.filter(n -> n.y == 0)
.mapToInt(n -> n.x)
.max()
.orElseThrow();
}
private int bfs(Node empty, int targetX, List<Node> nodes) {
int maxX = nodes.stream().mapToInt(n -> n.x).max().orElse(0);
int maxY = nodes.stream().mapToInt(n -> n.y).max().orElse(0);
int width = maxX + 1;
int height = maxY + 1;
boolean[][] passable = new boolean[width][height];
for (Node node : nodes) {
passable[node.x][node.y] = node.used <= empty.size;
}
boolean[][] visited = new boolean[width][height];
Queue<Position> queue = new LinkedList<>();
queue.add(new Position(empty.x, empty.y, 0));
visited[empty.x][empty.y] = true;
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
while (!queue.isEmpty()) {
Position curr = queue.poll();
if (curr.x == targetX && curr.y == 0) {
return curr.steps;
}
for (int i = 0; i < 4; i++) {
int nx = curr.x + dx[i];
int ny = curr.y + dy[i];
if (nx >= 0 && nx < width && ny >= 0 && ny < height && !visited[nx][ny] && passable[nx][ny]) {
visited[nx][ny] = true;
queue.add(new Position(nx, ny, curr.steps + 1));
}
}
}
return -1;
}
static class Node {
final int x, y, size, used, avail;
public Node(int x, int y, int size, int used, int avail) {
this.x = x;
this.y = y;
this.size = size;
this.used = used;
this.avail = avail;
}
}
static class Position {
final int x, y, steps;
public Position(int x, int y, int steps) {
this.x = x;
this.y = y;
this.steps = steps;
}
}
}