adventofcode2022/day15/sol.py

71 lines
1.4 KiB
Python
Raw Normal View History

2022-12-15 05:26:18 +00:00
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
2022-12-15 06:47:37 +00:00
#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)
2022-12-15 05:26:18 +00:00
print(len(none))
2022-12-15 06:47:37 +00:00
2022-12-15 06:50:07 +00:00
objs = []
for sx,sy,bx,by in data:
dist = abs(sx-bx) + abs(sy-by)
objs.append((sx,sy,dist))
2022-12-15 06:47:37 +00:00
# find the closest sensor to x,y within dist
2022-12-15 06:50:07 +00:00
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
2022-12-15 06:47:37 +00:00
2022-12-15 06:50:07 +00:00
# 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()
2022-12-15 06:47:37 +00:00
x = 0
2022-12-15 06:50:07 +00:00
for a,b in ranges:
if b < x:
continue
if x < a:
print(x,y,"=",x*4000000+y)
2022-12-15 06:47:37 +00:00
break
2022-12-15 06:50:07 +00:00
x = b+1
if x > 4000000:
2022-12-15 06:47:37 +00:00
break
2022-12-15 06:50:07 +00:00
#try:
# d = max(mindist(x,y,objs=yobjs))
# x += d+1
#except ValueError:
# print(x,y)
# break
if y % 100000 == 0:
2022-12-15 06:47:37 +00:00
print(y)