-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22.groovy
143 lines (119 loc) · 4.94 KB
/
day22.groovy
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
import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode
class Point {
Integer x, y, z
Point(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
}
@EqualsAndHashCode
class Cuboid {
Integer xmin, xmax, ymin, ymax, zmin, zmax
Cuboid(Integer xmin, Integer xmax, Integer ymin, Integer ymax, Integer zmin, Integer zmax) {
this.xmin = xmin;
this.xmax = xmax;
this.ymin = ymin;
this.ymax = ymax;
this.zmin = zmin;
this.zmax = zmax;
}
def size() {
return (Long)(this.xmax - this.xmin + 1) * (this.ymax - this.ymin + 1) * (this.zmax - this.zmin + 1)
}
}
def bounded(xmin, xmax, ymin, ymax, zmin, zmax) {
final BOUNDS = 50
out = xmin > BOUNDS || xmax < -BOUNDS || ymin > BOUNDS || ymax < -BOUNDS || zmin > BOUNDS || zmax < -BOUNDS
return !out
}
def points_part1 = new HashSet<>();
ArrayList<Cuboid> cuboids = new ArrayList<>();
new File("input.txt").eachLine { line ->
def matcher = line =~ /^(off|on) x=(-?\d+)..(-?\d+),y=(-?\d+)..(-?\d+),z=(-?\d+)..(-?\d+)/
xmin=matcher[0][2] as Integer
xmax=matcher[0][3] as Integer
ymin=matcher[0][4] as Integer
ymax=matcher[0][5] as Integer
zmin=matcher[0][6] as Integer
zmax=matcher[0][7] as Integer
if (bounded(xmin, xmax, ymin, ymax, zmin, zmax)) {
for(Integer x in xmin..xmax) {
for(Integer y in ymin..ymax) {
for(Integer z in zmin..zmax) {
if (matcher[0][1] == 'on') {
points_part1.add(new Point(x, y, z))
} else {
points_part1.remove(new Point(x, y, z))
}
}
}
}
}
scannedAll = false
while (!scannedAll && cuboids.size()) {
for (Integer index in 0..cuboids.size() - 1) {
def cuboid = cuboids[index]
// new cuboid contains existing cuboid and can replace it
if (xmin <= cuboid.xmin && xmax >= cuboid.xmax &&
ymin <= cuboid.ymin && ymax >= cuboid.ymax &&
zmin <= cuboid.zmin && zmax >= cuboid.zmax) {
cuboids.remove(index)
break;
}
// new cuboid does not intersect with existing cuboid
if (! (xmin <= cuboid.xmax && xmax >= cuboid.xmin &&
ymin <= cuboid.ymax && ymax >= cuboid.ymin &&
zmin <= cuboid.zmax && zmax >= cuboid.zmin)) {
if (index == cuboids.size() - 1) {
scannedAll = true
}
continue;
}
// intersections will split cuboids
if (xmin >= cuboid.xmin + 1 && xmin <= cuboid.xmax) {
cuboids[index] = new Cuboid(cuboid.xmin, xmin - 1, cuboid.ymin, cuboid.ymax, cuboid.zmin, cuboid.zmax)
cuboids.add(new Cuboid(xmin, cuboid.xmax, cuboid.ymin, cuboid.ymax, cuboid.zmin, cuboid.zmax))
break
}
if (xmax >= cuboid.xmin && xmax < cuboid.xmax) {
cuboids[index] = new Cuboid(xmax + 1, cuboid.xmax, cuboid.ymin, cuboid.ymax, cuboid.zmin, cuboid.zmax)
cuboids.add(new Cuboid(cuboid.xmin, xmax, cuboid.ymin, cuboid.ymax, cuboid.zmin, cuboid.zmax))
break
}
if (ymin >= cuboid.ymin + 1 && ymin <= cuboid.ymax) {
cuboids[index] = new Cuboid(cuboid.xmin, cuboid.xmax, cuboid.ymin, ymin - 1, cuboid.zmin, cuboid.zmax)
cuboids.add(new Cuboid(cuboid.xmin, cuboid.xmax, ymin, cuboid.ymax, cuboid.zmin, cuboid.zmax))
break
}
if (ymax >= cuboid.ymin && ymax < cuboid.ymax) {
cuboids[index] = new Cuboid(cuboid.xmin, cuboid.xmax, ymax + 1, cuboid.ymax, cuboid.zmin, cuboid.zmax)
cuboids.add(new Cuboid(cuboid.xmin, cuboid.xmax, cuboid.ymin, ymax, cuboid.zmin, cuboid.zmax))
break
}
if (zmin >= cuboid.zmin + 1 && zmin <= cuboid.zmax) {
cuboids[index] = new Cuboid(cuboid.xmin, cuboid.xmax, cuboid.ymin, cuboid.ymax, cuboid.zmin, zmin - 1)
cuboids.add(new Cuboid(cuboid.xmin, cuboid.xmax, cuboid.ymin, cuboid.ymax, zmin, cuboid.zmax))
break
}
if (zmax >= cuboid.zmin && zmax < cuboid.zmax) {
cuboids[index] = new Cuboid(cuboid.xmin, cuboid.xmax, cuboid.ymin, cuboid.ymax, zmax + 1, cuboid.zmax)
cuboids.add(new Cuboid(cuboid.xmin, cuboid.xmax, cuboid.ymin, cuboid.ymax, cuboid.zmin, zmax))
break
}
if (index == cuboids.size() - 1) {
scannedAll = true
}
}
}
if (matcher[0][1] == 'on') {
cuboids.add(new Cuboid(xmin, xmax, ymin, ymax, zmin, zmax))
}
}
long total = 0;
for (c in cuboids) {
total += c.size()
}
println "Day 22, part 1: ${points_part1.size()}"
println "Day 22, part 2: ${total}"