day 15 python wip optimizations

main
magical 2022-12-14 22:50:07 -08:00
parent 5f76b4e69d
commit 2b0ef33a08
1 changed files with 39 additions and 18 deletions

View File

@ -23,27 +23,48 @@ for sx,sy,bx,by in data:
print(len(none)) print(len(none))
# find the closest sensor to x,y within dist objs = []
def mindist(x,y): for sx,sy,bx,by in data:
for sx,sy,bx,by in data: dist = abs(sx-bx) + abs(sy-by)
dx = abs(sx-bx) objs.append((sx,sy,dist))
dy = abs(sy-by)
dist = dx+dy
dx = abs(x-sx)
dy = abs(y-sy)
d2 = dx+dy
if d2 <= dist:
yield dist-d2
for y in range(0, 4000000+1): # 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 x = 0
while x <= 4000000: for a,b in ranges:
for d in mindist(x,y): if b < x:
x += d+1 continue
if x < a:
print(x,y,"=",x*4000000+y)
break break
else: x = b+1
print(x,y) if x > 4000000:
break break
if y % 10000 == 0: #try:
# d = max(mindist(x,y,objs=yobjs))
# x += d+1
#except ValueError:
# print(x,y)
# break
if y % 100000 == 0:
print(y) print(y)