You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

94 lines
2.2 KiB

#!/usr/bin/env python3
import sys, math
import numpy as np
def rotations(x,y,z):
r = [
# x is facing x
[x, y, z],
[x, -z, y],
[x, -y, -z],
[x, z, -y],
# x is facing -x
[-x, -y, z],
[-x, -z, -y],
[-x, y, -z],
[-x, z, y],
# x is facing y
[-z, x, -y],
[y, x, -z],
[z, x, y],
[-y, x, z],
# x is facing -y
[z, -x, -y],
[y, -x, z],
[-z, -x, y],
[-y, -x, -z],
# x is facing z
[-y, -z, x],
[z, -y, x],
[y, z, x],
[-z, y, x],
# x is facing -z
[z, y, -x],
[-y, z, -x],
[-z, -y, -x],
[y, -z, -x]
]
for point in r:
yield np.array(point)
def overlap(s1,s2,off):
se1 = set([tuple(p) for p in s1])
se2 = set([tuple(p-off) for p in s2])
return len(se1.intersection(se2))
def test(s1,s2):
rot = [[r for r in rotations(*p)] for p in s2]
rot2 = [[r[i] for r in rot] for i in range(24)]
mc = 0
for i in range(24):
for p1 in s1:
for p2 in rot2[i]:
dif = p2 - p1
com = overlap(s1,rot2[i],dif)
if com > mc:
mc = com
if mc >= 12:
return [r - dif for r in rot2[i]], dif
return None, None
if __name__ == '__main__':
nums = [[np.array([i for i in map(int,n.split(','))]) for n in line.split('\n')[1:] if n != ''] for line in open(sys.argv[1]).read().split('\n\n')]
scanners = []
matched = {}
matched[0] = nums[0]
al = set([tuple(i) for i in matched[0]])
while len(matched) != len(nums):
for i in range(len(nums)):
if i in matched: continue
r = None
r, d = test(al,nums[i])
scanners.append(d)
if r != None:
matched[i] = r
al = al.union(set([tuple(i) for i in matched[i]]))
break
m = 0
for a in scanners:
for b in scanners:
n = sum(abs(a[i] - b[i]) for i in range(3))
if n > m:
m = n
# challenge 1
res1 = str(len(al))
print("challenge 1:" + "\n" + res1 + "\n")
# challenge 2
res2 = str(m)
print("challenge 2:" + "\n" + res2 + "\n")