-
Notifications
You must be signed in to change notification settings - Fork 1
/
19_beacon_scanner.py
76 lines (58 loc) · 1.74 KB
/
19_beacon_scanner.py
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
from utils import *
from itertools import combinations
from functools import reduce
# https://stackoverflow.com/a/16467849/458723
def roll(v):
'''Roll over X 90 degree counter-clockwise'''
return (v[0],v[2],-v[1])
def turn(v):
'''Turn over Z 90 degree counter-clockwise'''
return (-v[1],v[0],v[2])
def orientations(v):
'''All 24 orientations'''
for around_x in range(4):
for around_z in range(4):
yield v
v = turn(v)
v = roll(v)
v = roll(turn(v))
for around_z in range(4):
yield v
v = turn(v)
v = roll(roll(v))
for around_z in range(4):
yield v
v = turn(v)
def deltav(a, b):
return [y-x for x, y in zip(a, b)]
def addv(a, b):
return tuple(x+y for x, y in zip(a, b))
def shift(a, delta):
return [addv(x, delta) for x in a]
def find_delta(a: set, b: list):
for orient in zip(*[orientations(x) for x in b]):
for x in orient:
for y in a:
delta = deltav(x, y)
shifted = set(shift(orient, delta))
if len(a & shifted) >= 12:
a |= shifted
return a, delta
def manh(a, b):
return sum(abs(x-y) for x, y in zip(a, b))
scans = [x[1:] for x in read('19_input.txt')]
scanners = [(0, 0, 0)]
beacons = {tuple(x) for x in scans[0]}
merged = {0}
while len(merged) < len(scans):
for i in range(len(scans)):
if i in merged:
continue
print(len(merged), i)
tmp = find_delta(beacons, scans[i])
if tmp is not None:
beacons, s = tmp
merged.add(i)
scanners.append(s)
print('Star 1:', len(beacons))
print('Star 2:', max(manh(a, b) for a, b in combinations(scanners, 2)))