Day 15
This commit is contained in:
@@ -1,35 +1,26 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import re
|
import re
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
lines = (x.strip() for x in open("input.txt"))
|
lines = (x.strip() for x in open("input.txt"))
|
||||||
lines = [tuple(map(int, re.search(r"(-?\d+).+?(-?\d+).+?(-?\d+).+?(-?\d+)", line).groups())) for line in lines]
|
# all lines of possible positions
|
||||||
|
# mx+b; tl/br have m=1; tr/bl have m=-1
|
||||||
|
tl, tr, bl, br = set(), set(), set(), set()
|
||||||
|
stations = []
|
||||||
|
|
||||||
for target in range(0, 4000000): #3186981
|
for line in lines:
|
||||||
if target % 40000 == 0:
|
x, y, bx, by = map(int, re.search(r"(-?\d+).+?(-?\d+).+?(-?\d+).+?(-?\d+)", line).groups())
|
||||||
print(target/4000000)
|
dist = abs(x-bx) + abs(y-by) + 1
|
||||||
intervals = []
|
tl.add(y-dist+x)
|
||||||
known_targets = set()
|
br.add(y+dist+x)
|
||||||
|
bl.add(y+dist-x)
|
||||||
|
tr.add(y-dist-x)
|
||||||
|
stations.append((x, y, dist))
|
||||||
|
|
||||||
for line in lines:
|
for bu, bd in ((a, b) for a in tl & br for b in tr & bl):
|
||||||
x, y, a, b = line
|
# -x+bu=x+bd <=> x=(bd-bu)/2
|
||||||
dist = abs(x-a) + abs(y-b)
|
x = (bu-bd)//2
|
||||||
width = dist-abs(y-target)
|
y = x+bd
|
||||||
if width >= 0:
|
|
||||||
intervals.append((x-width, x+width))
|
|
||||||
if b == target:
|
|
||||||
known_targets.add(a)
|
|
||||||
if y == target:
|
|
||||||
known_targets.add(x)
|
|
||||||
|
|
||||||
|
if all(abs(x-sx)+abs(y-sy) >= dist for sx, sy, dist in stations):
|
||||||
intervals.sort(key=lambda x: x[0])
|
print(x*4000000+y)
|
||||||
merged = [intervals[0]]
|
|
||||||
for i in intervals[1:]:
|
|
||||||
if merged[-1][1] >= i[0]:
|
|
||||||
merged[-1] = (merged[-1][0], max(merged[-1][1], i[1]))
|
|
||||||
else:
|
|
||||||
merged.append(i)
|
|
||||||
|
|
||||||
if len(merged) > 1:
|
|
||||||
print(target, merged, (merged[0][1]+1)*4000000+target)
|
|
||||||
break
|
|
||||||
|
|||||||
Reference in New Issue
Block a user