-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.go
167 lines (153 loc) · 3.45 KB
/
19.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
type pos struct {
x, y, z int
}
func (p pos) move(dx, dy, dz int) pos {
return pos{p.x + dx, p.y + dy, p.z + dz}
}
func (p pos) rotate(times int) pos {
switch times {
case 0:
return p
case 1:
return pos{-p.y, p.x, p.z}
case 2:
return pos{-p.x, -p.y, p.z}
case 3:
return pos{p.y, -p.x, p.z}
}
panic(fmt.Sprintf("incorrect rotation %d", times))
}
func (p pos) direct(to int) pos {
switch to {
case 0:
return p
case 1:
return pos{p.x, -p.y, p.z}
case 2:
return pos{p.x, -p.z, p.y}
case 3:
return pos{-p.y, -p.z, p.x}
case 4:
return pos{-p.x, -p.z, -p.y}
case 5:
return pos{p.y, -p.z, -p.x}
}
panic(fmt.Sprintf("incorrect direction %d", to))
}
func (p pos) distance(other pos) int {
return int(abs(int64(p.x-other.x)) + abs(int64(p.y-other.y)+abs(int64(p.z-other.z))))
}
func parse(s string) pos {
parts := strings.Split(s, ",")
x, _ := strconv.ParseInt(parts[0], 10, 32)
y, _ := strconv.ParseInt(parts[1], 10, 32)
z, _ := strconv.ParseInt(parts[2], 10, 32)
return pos{int(x), int(y), int(z)}
}
type scanner struct {
n int
p pos
beacons []pos
}
var reNum = regexp.MustCompile("\\-?\\d+")
func parseScanner(s *bufio.Scanner) (scanner, error) {
line := s.Text()
if line[0:3] != "---" {
return scanner{}, fmt.Errorf("invalid input ", line)
}
matches := reNum.FindAllString(line, -1)
n, _ := strconv.ParseInt(matches[0], 10, 32)
beacons := []pos{}
s.Scan()
line = s.Text()
for line != "" && len(line) > 0 && len(reNum.FindAllString(line, -1)) > 1 {
beacons = append(beacons, parse(line))
s.Scan()
line = s.Text()
}
return scanner{int(n), pos{0, 0, 0}, beacons}, nil
}
func match(beacons map[pos]bool, s scanner) bool {
for beacon, _ := range beacons {
for _, p := range s.beacons {
mapped := []pos{}
fit := 0
for _, b := range s.beacons {
mb := b.move(beacon.x-p.x, beacon.y-p.y, beacon.z-p.z)
mapped = append(mapped, mb)
if beacons[mb] {
fit++
}
}
if fit >= 12 {
for _, m := range mapped {
beacons[m] = true
}
s.p = pos{beacon.x - p.x, beacon.y - p.y, beacon.z - p.z}
return true
}
}
}
return false
}
func (s scanner) transform(rotation, direction int) scanner {
t := scanner{s.n, s.p, []pos{}}
for _, beacon := range s.beacons {
t.beacons = append(t.beacons, beacon.rotate(rotation).direct(direction))
}
return t
}
func matchAll(beacons map[pos]bool, s scanner) bool {
for rotation := 0; rotation < 4; rotation++ {
for direction := 0; direction < 6; direction++ {
ts := s.transform(rotation, direction)
if match(beacons, ts) {
s.p = ts.p
return true
}
}
}
return false
}
func main() {
s := bufio.NewScanner(os.Stdin)
scanners := make([]scanner, 0)
for s.Scan() {
sc, err := parseScanner(s)
if err != nil {
panic(err)
}
scanners = append(scanners, sc)
}
unique := map[pos]bool{}
for _, b := range scanners[0].beacons {
unique[b] = true
}
processed := map[int]bool{0: true}
for len(processed) < len(scanners) {
for _, sc := range scanners {
if !processed[sc.n] && matchAll(unique, sc) {
processed[sc.n] = true
}
}
fmt.Println("processed", len(processed), "of", len(scanners),
"unique beacons", len(unique))
}
fmt.Println(len(unique))
distance := 0
for i := 0; i < len(scanners); i++ {
for j := i + 1; j < len(scanners); j++ {
distance = maxi(distance, scanners[i].p.distance(scanners[j].p))
}
}
fmt.Println(distance)
}