adventofcode2022/day15/sol.py

71 lines
1.4 KiB
Python

data = []
for line in open("input"):
words = line.replace(":", "").replace(",","").split()
coords = [int(w[w.index('=')+1:]) for w in words if '=' in w]
data.append(coords)
#print(data)
none = set()
Y = 10
Y = 2000000
for sx,sy,bx,by in data:
dx = abs(sx-bx)
dy = abs(sy-by)
dist = dx+dy
y = Y-sy
#for x in range(-dist,dist+1):
# if abs(x)+abs(y) <= dist:
# p = (sx+x, sy+y)
# if p != (bx,by):
# none.add(sx+x)
print(len(none))
objs = []
for sx,sy,bx,by in data:
dist = abs(sx-bx) + abs(sy-by)
objs.append((sx,sy,dist))
# find the closest sensor to x,y within dist
def mindist(x,y, objs=objs):
for sx,sy,dist in yobjs:
d = abs(x-sx) + abs(y-sy)
if d <= dist:
yield dist-d
# y=2573243
Y = 2500000
Y = 0
objs.sort()
for y in range(Y, 4000000+1):
ranges = []
for sx,sy,dist in objs:
dy = abs(y-sy)
if dy <= dist:
dx = dist - dy
ranges.append((sx-dx,sx+dx))
ranges.sort()
x = 0
for a,b in ranges:
if b < x:
continue
if x < a:
print(x,y,"=",x*4000000+y)
break
x = b+1
if x > 4000000:
break
#try:
# d = max(mindist(x,y,objs=yobjs))
# x += d+1
#except ValueError:
# print(x,y)
# break
if y % 100000 == 0:
print(y)