-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay18.java
More file actions
205 lines (187 loc) · 5.73 KB
/
Day18.java
File metadata and controls
205 lines (187 loc) · 5.73 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.util.ArrayList;
import java.util.HashMap;
public class Day18 {
public static void main(String[] args) {
new Day18();
}
public Day18() {
ArrayList<String> input = ReadInput.read("res/input18.txt");
partOne(input);
partTwo(input);
}
private int partOneAnswer;
private void partOne(ArrayList<String> input) {
ArrayList<Cube> cubes = parseInput(input);
int sides = cubes.size()*6;
for(int i = 0; i < cubes.size(); i++) {
for(int j = i + 1; j < cubes.size(); j++) {
Cube c1 = cubes.get(i);
Cube c2 = cubes.get(j);
if(c1.x + 1 == c2.x && c1.y == c2.y && c1.z == c2.z)
sides -= 2;
else if(c1.x - 1 == c2.x && c1.y == c2.y && c1.z == c2.z)
sides -= 2;
else if(c1.x == c2.x && c1.y + 1 == c2.y && c1.z == c2.z)
sides -= 2;
else if(c1.x == c2.x && c1.y - 1 == c2.y && c1.z == c2.z)
sides -= 2;
else if(c1.x == c2.x && c1.y == c2.y && c1.z + 1 == c2.z)
sides -= 2;
else if(c1.x == c2.x && c1.y == c2.y && c1.z - 1 == c2.z)
sides -= 2;
}
}
this.partOneAnswer = sides;
System.out.println(sides);
}
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
int minZ = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int maxY = Integer.MIN_VALUE;
int maxZ = Integer.MIN_VALUE;
private void partTwo(ArrayList<String> input) {
ArrayList<Cube> cubes = parseInput(input);
for(Cube c : cubes) {
if(c.x > maxX)
maxX = c.x;
else if(c.x < minX)
minX = c.x;
if(c.y > maxY)
maxY = c.y;
else if(c.y < minY)
minY = c.y;
if(c.z > maxZ)
maxZ = c.z;
else if(c.z < minZ)
minZ = c.z;
}
// create map of each coordinate with lava or air
HashMap<String, Integer> map = new HashMap<String, Integer>();
for(int x = minX; x <= maxX; x++) {
for(int y = minY; y <= maxY; y++) {
for(int z = minZ; z <= maxZ; z++) {
// 0 = air, 1 = lava
if(containsCube(cubes, x, y, z))
map.put("" + x + "," + y + "," + z, 1);
else
map.put("" + x + "," + y + "," + z, 0);
}
}
}
// create map with all coordinates ouf air outside of lava
HashMap<String, Boolean> outerAir = new HashMap<String, Boolean>();
for(int x = minX; x <= maxX; x++) {
for(int y = minY; y <= maxY; y++) {
int z = minZ;
if(map.get("" + x + "," + y + "," + z) == 0)
findAdjacentAir(x, y, z, map, outerAir);
}
}
for(int x = minX; x <= maxX; x++) {
for(int y = minY; y <= maxY; y++) {
int z = maxZ;
if(map.get("" + x + "," + y + "," + z) == 0)
findAdjacentAir(x, y, z, map, outerAir);
}
}
for(int x = minX; x <= maxX; x++) {
for(int z = minZ; z <= maxZ; z++) {
int y = minY;
if(map.get("" + x + "," + y + "," + z) == 0)
findAdjacentAir(x, y, z, map, outerAir);
}
}
for(int x = minX; x <= maxX; x++) {
for(int z = minZ; z <= maxZ; z++) {
int y = maxY;
if(map.get("" + x + "," + y + "," + z) == 0)
findAdjacentAir(x, y, z, map, outerAir);
}
}
for(int y = minY; y <= maxY; y++) {
for(int z = minZ; z <= maxZ; z++) {
int x = minX;
if(map.get("" + x + "," + y + "," + z) == 0)
findAdjacentAir(x, y, z, map, outerAir);
}
}
for(int y = minY; y <= maxY; y++) {
for(int z = minZ; z <= maxZ; z++) {
int x = maxX;
if(map.get("" + x + "," + y + "," + z) == 0)
findAdjacentAir(x, y, z, map, outerAir);
}
}
// create map of coordinates of air inside of lava
ArrayList<Cube> innerAir = new ArrayList<Cube>();
for(int x = minX; x <= maxX; x++) {
for(int y = minY; y <= maxY; y++) {
for(int z = minZ; z < maxZ; z++) {
if(outerAir.get("" + x + "," + y + "," + z) != null)
continue;
if(map.get("" + x + "," + y + "," + z) == 0)
innerAir.add(new Cube(x, y, z));
}
}
}
// calculate surface area of air cubes, like in part one (but for air)
int airSides = innerAir.size()*6;
for(int i = 0; i < innerAir.size(); i++) {
for(int j = i + 1; j < innerAir.size(); j++) {
Cube c1 = innerAir.get(i);
Cube c2 = innerAir.get(j);
if(c1.x + 1 == c2.x && c1.y == c2.y && c1.z == c2.z)
airSides -= 2;
else if(c1.x - 1 == c2.x && c1.y == c2.y && c1.z == c2.z)
airSides -= 2;
else if(c1.x == c2.x && c1.y + 1 == c2.y && c1.z == c2.z)
airSides -= 2;
else if(c1.x == c2.x && c1.y - 1 == c2.y && c1.z == c2.z)
airSides -= 2;
else if(c1.x == c2.x && c1.y == c2.y && c1.z + 1 == c2.z)
airSides -= 2;
else if(c1.x == c2.x && c1.y == c2.y && c1.z - 1 == c2.z)
airSides -= 2;
}
}
System.out.println(partOneAnswer - airSides);
}
private ArrayList<Cube> parseInput(ArrayList<String> input) {
ArrayList<Cube> cubes = new ArrayList<Cube>();
for(String line : input) {
cubes.add(new Cube(Integer.parseInt(line.split(",")[0]), Integer.parseInt(line.split(",")[1]), Integer.parseInt(line.split(",")[2])));
}
return cubes;
}
private boolean containsCube(ArrayList<Cube> cubes, int x, int y, int z) {
for(Cube c : cubes) {
if(c.x == x && c.y == y && c.z == z)
return true;
}
return false;
}
private void findAdjacentAir(int x, int y, int z, HashMap<String, Integer> map, HashMap<String, Boolean> outerAir) {
if(x > maxX || x < minX || y > maxY || y < minY || z > maxZ || z < minZ)
return;
if(outerAir.get("" + x + "," + y + "," + z) != null)
return;
if(map.get("" + x + "," + y + "," + z) == 1)
return;
outerAir.put("" + x + "," + y + "," + z, true);
findAdjacentAir(x + 1, y, z, map, outerAir);
findAdjacentAir(x - 1, y, z, map, outerAir);
findAdjacentAir(x, y + 1, z, map, outerAir);
findAdjacentAir(x, y - 1, z, map, outerAir);
findAdjacentAir(x, y, z + 1, map, outerAir);
findAdjacentAir(x, y, z - 1, map, outerAir);
}
private class Cube {
public int x, y, z;
public Cube(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
}