-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (146 loc) · 3.85 KB
/
main.go
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
package main
import (
"fmt"
"github.com/jdrst/adventofgo/util"
)
type coord struct {
x, y, z int
}
type cuboid struct {
min, max coord
}
func main() {
fmt.Printf("First part: %v\n", partOne(util.ReadFile("input.txt")))
fmt.Printf("Second part: %v\n", partTwo(util.ReadFile("input.txt")))
}
func partOne(file util.File) int {
lines := file.AsLines()
cubes := []cuboid{}
var xmin, xmax, ymin, ymax, zmin, zmax int
var state string
for _, l := range lines {
fmt.Sscanf(string(l), "%v x=%d..%d,y=%d..%d,z=%d..%d", &state, &xmin, &xmax, &ymin, &ymax, &zmin, &zmax)
if xmax < -50 || ymax < -50 || zmax < -50 || xmin > 50 || ymin > 50 || zmin > 50 {
continue
}
xmin = max(xmin, -50)
xmax = min(xmax, 50)
ymin = max(ymin, -50)
ymax = min(ymax, 50)
zmin = max(zmin, -50)
zmax = min(zmax, 50)
min := coord{xmin - 1, ymin - 1, zmin - 1}
max := coord{xmax, ymax, zmax}
current := cuboid{min, max}
newCubes := []cuboid{}
for _, c := range cubes {
newCubes = append(newCubes, c.except(current)...)
}
if state == "on" {
newCubes = append(newCubes, current)
}
cubes = newCubes
}
return sumCubes(cubes)
}
func partTwo(file util.File) int {
lines := file.AsLines()
cubes := []cuboid{}
var xmin, xmax, ymin, ymax, zmin, zmax int
var state string
for _, l := range lines {
fmt.Sscanf(string(l), "%v x=%d..%d,y=%d..%d,z=%d..%d", &state, &xmin, &xmax, &ymin, &ymax, &zmin, &zmax)
min := coord{xmin - 1, ymin - 1, zmin - 1}
max := coord{xmax, ymax, zmax}
current := cuboid{min, max}
newCubes := []cuboid{}
for _, c := range cubes {
newCubes = append(newCubes, c.except(current)...)
}
if state == "on" {
newCubes = append(newCubes, current)
}
cubes = newCubes
}
return sumCubes(cubes)
}
func (c *cuboid) volume() int {
return util.Delta(c.max.x, c.min.x) * util.Delta(c.max.y, c.min.y) * util.Delta(c.max.z, c.min.z)
}
func between(num, x, y int) bool {
if num >= x && num <= y {
return true
}
return false
}
func (a cuboid) intersects(b cuboid) bool {
return (between(b.min.x, a.min.x, a.max.x) || between(a.min.x, b.min.x, b.max.x)) && (between(b.min.y, a.min.y, a.max.y) || between(a.min.y, b.min.y, b.max.y)) && (between(b.min.z, a.min.z, a.max.z) || between(a.min.z, b.min.z, b.max.z))
}
func (b cuboid) isWithin(a cuboid) bool {
return between(b.min.x, a.min.x, a.max.x) && between(b.min.y, a.min.y, a.max.y) && between(b.min.z, a.min.z, a.max.z) && between(b.max.x, a.min.x, a.max.x) && between(b.max.y, a.min.y, a.max.y) && between(b.max.z, a.min.z, a.max.z)
}
func (a cuboid) except(b cuboid) []cuboid {
res := []cuboid{}
if a.isWithin(b) {
return res
}
if a.intersects(b) {
minX := min(a.max.x, b.max.x)
maxX := max(a.min.x, b.min.x)
//something right
if minX < a.max.x {
res = append(res, cuboid{coord{minX, a.min.y, a.min.z}, a.max})
a.max.x = minX
}
//something left
if maxX > a.min.x {
res = append(res, cuboid{a.min, coord{maxX, a.max.y, a.max.z}})
a.min.x = maxX
}
minY := min(a.max.y, b.max.y)
maxY := max(a.min.y, b.min.y)
//something over
if minY < a.max.y {
res = append(res, cuboid{coord{a.min.x, minY, a.min.z}, a.max})
a.max.y = minY
}
//something under
if maxY > a.min.y {
res = append(res, cuboid{a.min, coord{a.max.x, maxY, a.max.z}})
a.min.y = maxY
}
minZ := min(a.max.z, b.max.z)
maxZ := max(a.min.z, b.min.z)
//something in back
if minZ < a.max.z {
res = append(res, cuboid{coord{a.min.x, a.min.y, minZ}, a.max})
a.max.z = minZ
}
//something in front
if maxZ > a.min.z {
res = append(res, cuboid{a.min, coord{a.max.x, a.max.y, maxZ}})
a.min.z = maxZ
}
return res
}
return []cuboid{a}
}
func sumCubes(cubes []cuboid) int {
on := 0
for _, c := range cubes {
on += c.volume()
}
return on
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
func min(x, y int) int {
if x > y {
return y
}
return x
}