day 15 python wip optimizations
parent
5f76b4e69d
commit
2b0ef33a08
55
day15/sol.py
55
day15/sol.py
|
@ -23,27 +23,48 @@ for sx,sy,bx,by in data:
|
|||
|
||||
print(len(none))
|
||||
|
||||
# find the closest sensor to x,y within dist
|
||||
def mindist(x,y):
|
||||
objs = []
|
||||
for sx,sy,bx,by in data:
|
||||
dx = abs(sx-bx)
|
||||
dy = abs(sy-by)
|
||||
dist = dx+dy
|
||||
dx = abs(x-sx)
|
||||
dy = abs(y-sy)
|
||||
d2 = dx+dy
|
||||
if d2 <= dist:
|
||||
yield dist-d2
|
||||
dist = abs(sx-bx) + abs(sy-by)
|
||||
objs.append((sx,sy,dist))
|
||||
|
||||
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
|
||||
while x <= 4000000:
|
||||
for d in mindist(x,y):
|
||||
x += d+1
|
||||
for a,b in ranges:
|
||||
if b < x:
|
||||
continue
|
||||
if x < a:
|
||||
print(x,y,"=",x*4000000+y)
|
||||
break
|
||||
else:
|
||||
print(x,y)
|
||||
x = b+1
|
||||
if x > 4000000:
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue