-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.go
240 lines (216 loc) · 5.21 KB
/
day19.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"bufio"
"fmt"
"io"
"math"
"os"
"runtime/pprof"
"sort"
)
type nodeType int
const (
ntScanner nodeType = iota
ntBacon
)
type Nodes = map[coord3]nodeType
type ax string
const (
axX ax = "x"
axY ax = "y"
axZ ax = "z"
)
type coord3 struct {
x, y, z int
}
type scanner struct {
num int
beacons []coord3
}
func day19input(input io.Reader) []scanner {
s := bufio.NewScanner(input)
var scnrs []scanner
for {
if !s.Scan() {
break
}
scnr := scanner{}
fmt.Sscanf(s.Text(), "--- scanner %d ---", &scnr.num)
for s.Scan() {
if s.Text() == "" {
break
}
var bacon coord3
fmt.Sscanf(s.Text(), "%d,%d,%d", &bacon.x, &bacon.y, &bacon.z)
scnr.beacons = append(scnr.beacons, bacon)
}
scnrs = append(scnrs, scnr)
}
return scnrs
}
func rotate(deg int, ax ax, nodes Nodes) Nodes {
if deg == 0 || deg%360 == 0 {
return nodes
}
// https://www.khanacademy.org/computing/computer-programming/programming-games-visualizations/programming-3d-shapes/a/rotating-3d-shapes
theta := float64(deg) * (math.Pi / 180)
var sinTheta = math.Sin(theta)
var cosTheta = math.Cos(theta)
newNodes := make(Nodes, len(nodes))
for node, typ := range nodes {
var newnode coord3
switch ax {
case axZ:
newnode.x = int(math.Round(float64(node.x)*cosTheta - float64(node.y)*sinTheta))
newnode.y = int(math.Round(float64(node.y)*cosTheta + float64(node.x)*sinTheta))
newnode.z = node.z
case axX:
newnode.y = int(math.Round(float64(node.y)*cosTheta - float64(node.z)*sinTheta))
newnode.z = int(math.Round(float64(node.z)*cosTheta + float64(node.y)*sinTheta))
newnode.x = node.x
case axY:
newnode.x = int(math.Round(float64(node.x)*cosTheta + float64(node.z)*sinTheta))
newnode.z = int(math.Round(float64(node.z)*cosTheta - float64(node.x)*sinTheta))
newnode.y = node.y
default:
panic("unknown ax")
}
newNodes[newnode] = typ
}
return newNodes
}
type rot struct {
ax ax
angle int
}
func (r rot) String() string {
return fmt.Sprintf("rot: %s: %v", r.ax, r.angle)
}
func rotates(nodes Nodes, rots [2]rot) Nodes {
for _, rot := range rots {
nodes = rotate(rot.angle, rot.ax, nodes)
}
return nodes
}
func toSlice(nodes Nodes) (result []coord3) {
for node := range nodes {
result = append(result, node)
}
sort.Slice(result, func(i, j int) bool {
if result[i].x != result[j].x {
return result[i].x < result[j].x
}
if result[i].y != result[j].y {
return result[i].y < result[j].y
}
return result[i].z < result[j].z
})
return
}
func combos() (result [][2]rot) {
// no idea why this works
// too hard
// but it works
for i := 0; i < 3; i++ {
result = append(result, [2]rot{{axX, 0}, {axY, 0}})
result = append(result, [2]rot{{axX, 0}, {axY, 90}})
result = append(result, [2]rot{{axZ, 90}, {axY, 0}})
result = append(result, [2]rot{{axX, 180}, {axY, 0}}) // ax flip
result = append(result, [2]rot{{axX, 0}, {axY, 90}})
result = append(result, [2]rot{{axZ, 90}, {axY, 0}})
}
return
}
func distance(items []coord3) int {
dist := 0
for i, itemA := range items {
for _, itemB := range items[i+1:] {
val := abs(itemA.x-itemB.x) + abs(itemA.y-itemB.y) + abs(itemA.z-itemB.z)
if val > dist {
dist = val
}
}
}
return dist
}
func day19a(input []scanner) (int, int) {
f, _ := os.Create("/tmp/aoc_profile.txt")
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
grid := Nodes{coord3{0, 0, 0}: ntScanner}
for _, v := range input[0].beacons {
grid[v] = ntBacon
}
others := make([]coord3, 0, len(grid))
left := input[1:]
outer:
for len(left) > 0 {
change := false
for _, combo := range combos() {
grid = rotates(grid, combo)
for i, scnr := range left {
for baconA, typ := range grid {
if typ == ntScanner {
continue
}
beacons := scnr.beacons
for _, baconBa := range beacons {
match := 1
others = others[:0]
for _, baconBb := range beacons {
if baconBa == baconBb {
continue
}
deltax := baconBb.x - baconBa.x
deltay := baconBb.y - baconBa.y
deltaz := baconBb.z - baconBa.z
n := coord3{x: baconA.x + deltax, y: baconA.y + deltay,
z: baconA.z + deltaz}
if _, ok := grid[n]; ok {
match++
} else {
others = append(others, n)
}
}
if match >= 12 {
for _, other := range others {
grid[other] = ntBacon
}
left[i], left[len(left)-1] = left[len(left)-1], left[i]
left = left[:len(left)-1]
// fmt.Printf("num: %v %v %v left: %v\n", scnr.num, match, combo, len(left))
scanner := coord3{x: baconA.x - baconBa.x, y: baconA.y - baconBa.y,
z: baconA.z - baconBa.z}
change = true
grid[scanner] = ntScanner
continue outer
}
}
}
}
}
if !change {
break
}
}
var scanners []coord3
for node, typ := range grid {
if typ == ntScanner {
scanners = append(scanners, node)
}
}
if len(left) > 0 {
panic("unmatched beacons")
}
return len(grid) - len(input), distance(scanners)
}
func Day19() {
f, err := os.Open("input/day19.txt")
if err != nil {
panic(err)
}
defer f.Close()
resultA, resultB := day19a(day19input(f))
fmt.Printf("day 19: beacons: %d distance: %d\n", resultA, resultB)
}