-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
262 lines (222 loc) · 5.97 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/devries/advent_of_code_2021/utils"
)
func main() {
f, err := os.Open("../inputs/day19.txt")
utils.Check(err, "error opening input file")
defer f.Close()
r := solve(f)
fmt.Println(r)
}
func solve(r io.Reader) int {
lines := utils.ReadLines(r)
beaconSets := parseInput(lines)
unprocessedIndex := 1
for origin := 0; origin < len(beaconSets)-1; origin++ {
ma := findMetrics(beaconSets[origin])
for i := unprocessedIndex; i < len(beaconSets); i++ {
mb := findMetrics(beaconSets[i])
matches := findMatches(ma, mb)
if countOverlapPoints(matches) >= 12 {
transformMap := make(map[Transform]int)
maxTransforms := 0
for _, m := range matches {
pa := m.MetricA.A
pb := reorient[m.Orientation](m.MetricB.A)
offset := Point{pb.X - pa.X, pb.Y - pa.Y, pb.Z - pa.Z}
transforms := transformMap[Transform{m.Orientation, offset}] + 1
transformMap[Transform{m.Orientation, offset}] = transforms
if transforms > maxTransforms {
maxTransforms = transforms
}
}
if maxTransforms > 0 {
for k, v := range transformMap {
if v == maxTransforms {
// Transform found
for j := 0; j < len(beaconSets[i]); j++ {
beaconSets[i][j] = k.Apply(beaconSets[i][j])
}
beaconSets[unprocessedIndex], beaconSets[i] = beaconSets[i], beaconSets[unprocessedIndex]
unprocessedIndex++
}
}
}
}
}
}
allPoints := make(map[Point]bool)
for i := 0; i < len(beaconSets); i++ {
for j := 0; j < len(beaconSets[i]); j++ {
allPoints[beaconSets[i][j]] = true
}
}
total := len(allPoints)
return total
}
type Point struct {
X int
Y int
Z int
}
func parseInput(lines []string) [][]Point {
beaconSets := make([][]Point, 0)
beacons := make([]Point, 0)
for i, line := range lines {
if strings.HasPrefix(line, "---") && i > 0 {
beaconSets = append(beaconSets, beacons)
beacons = make([]Point, 0)
continue
}
var p Point
n, _ := fmt.Sscanf(line, "%d,%d,%d", &p.X, &p.Y, &p.Z)
if n == 3 {
beacons = append(beacons, p)
}
}
beaconSets = append(beaconSets, beacons)
return beaconSets
}
var reorient = [](func(p Point) Point){
func(p Point) Point { return Point{p.X, p.Y, p.Z} },
func(p Point) Point { return Point{p.Y, -p.X, p.Z} },
func(p Point) Point { return Point{-p.X, -p.Y, p.Z} },
func(p Point) Point { return Point{-p.Y, p.X, p.Z} },
func(p Point) Point { return Point{-p.X, p.Y, -p.Z} },
func(p Point) Point { return Point{-p.Y, -p.X, -p.Z} },
func(p Point) Point { return Point{p.X, -p.Y, -p.Z} },
func(p Point) Point { return Point{p.Y, p.X, -p.Z} },
func(p Point) Point { return Point{p.X, -p.Z, p.Y} },
func(p Point) Point { return Point{-p.Z, -p.X, p.Y} },
func(p Point) Point { return Point{-p.X, p.Z, p.Y} },
func(p Point) Point { return Point{p.Z, p.X, p.Y} },
func(p Point) Point { return Point{-p.X, -p.Z, -p.Y} },
func(p Point) Point { return Point{-p.Z, p.X, -p.Y} },
func(p Point) Point { return Point{p.X, p.Z, -p.Y} },
func(p Point) Point { return Point{p.Z, -p.X, -p.Y} },
func(p Point) Point { return Point{p.Y, p.Z, p.X} },
func(p Point) Point { return Point{-p.Z, p.Y, p.X} },
func(p Point) Point { return Point{-p.Y, -p.Z, p.X} },
func(p Point) Point { return Point{p.Z, -p.Y, p.X} },
func(p Point) Point { return Point{p.Y, -p.Z, -p.X} },
func(p Point) Point { return Point{p.Z, p.Y, -p.X} },
func(p Point) Point { return Point{-p.Y, p.Z, -p.X} },
func(p Point) Point { return Point{-p.Z, -p.Y, -p.X} },
}
type ThreeDistance [3]int
func (t *ThreeDistance) Len() int { return 3 }
func (t *ThreeDistance) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t *ThreeDistance) Less(i, j int) bool { return t[i] < t[j] }
type Metric struct {
Distances ThreeDistance
Distance int
DeltaX int
DeltaY int
DeltaZ int
A Point
B Point
}
type Metrics []Metric
func (m Metrics) Len() int { return len(m) }
func (m Metrics) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m Metrics) Less(i, j int) bool { return m[i].Distance < m[j].Distance }
func (d ThreeDistance) String() string {
return fmt.Sprintf("%d,%d,%d", d[0], d[1], d[2])
}
func findMetrics(beacons []Point) Metrics {
res := make(Metrics, 0)
for i := 0; i < len(beacons)-1; i++ {
for j := i + 1; j < len(beacons); j++ {
a := beacons[i]
b := beacons[j]
dx := b.X - a.X
dy := b.Y - a.Y
dz := b.Z - a.Z
dxm := abs(dx)
dym := abs(dy)
dzm := abs(dz)
m := Metric{ThreeDistance{dxm, dym, dzm}, dxm + dym + dzm, dx, dy, dz, a, b}
sort.Sort(&m.Distances)
res = append(res, m)
}
}
sort.Sort(res)
return res
}
func abs(i int) int {
if i < 0 {
return -i
}
return i
}
type Match struct {
MetricA Metric
MetricB Metric
Orientation int
}
func findMatches(a Metrics, b Metrics) []Match {
res := make([]Match, 0)
for i, j := 0, 0; i < len(a) || j < len(b); {
if i >= len(a) || j >= len(b) {
break
}
ma := a[i]
mb := b[j]
if ma.Distance < mb.Distance {
i++
continue
}
if mb.Distance > ma.Distance {
j++
continue
}
// Same distance
if ma.Distances == mb.Distances {
// Same components
pa := Point{ma.DeltaX, ma.DeltaY, ma.DeltaZ}
pb := Point{mb.DeltaX, mb.DeltaY, mb.DeltaZ}
for k, f := range reorient {
if pa == f(pb) {
res = append(res, Match{ma, mb, k})
}
}
}
if i < len(a)-1 && j < len(b)-1 {
if a[i+1].Distance < b[j+1].Distance {
i++
} else {
j++
}
} else if j < len(b)-1 {
j++
} else if i < len(a)-1 {
i++
} else {
break
}
}
return res
}
func countOverlapPoints(matches []Match) int {
res := make(map[Point]bool)
for _, m := range matches {
res[m.MetricA.A] = true
res[m.MetricA.B] = true
}
return len(res)
}
type Transform struct {
Orientation int
Offset Point
}
func (t Transform) Apply(p Point) Point {
prot := reorient[t.Orientation](p)
pprime := Point{prot.X - t.Offset.X, prot.Y - t.Offset.Y, prot.Z - t.Offset.Z}
return pprime
}