day 14 python more cleanup
parent
80cf54d600
commit
37e5ab92e0
108
day14/sol.py
108
day14/sol.py
|
@ -2,8 +2,6 @@
|
|||
source = (500,0)
|
||||
|
||||
data = []
|
||||
xs = []
|
||||
ys = []
|
||||
for line in open("input"):
|
||||
path = []
|
||||
for point in line.replace("->", " ").split():
|
||||
|
@ -21,38 +19,29 @@ Y1 = 1
|
|||
|
||||
def put(x,y,c):
|
||||
global X0, X1, Y0, Y1
|
||||
if x < X0:
|
||||
X0 = x
|
||||
if x >= X1:
|
||||
X1 = x + 1
|
||||
if y < Y0:
|
||||
Y0 = y
|
||||
if y >= Y1:
|
||||
Y1 = y + 1
|
||||
if x < X0: X0 = x
|
||||
if x >= X1: X1 = x + 1
|
||||
if y < Y0: Y0 = y
|
||||
if y >= Y1: Y1 = y + 1
|
||||
grid[(x,y)] = c
|
||||
|
||||
def get(x,y):
|
||||
if (x,y) in grid:
|
||||
return grid[(x,y)]
|
||||
return '.'
|
||||
return grid.get((x,y), '.')
|
||||
|
||||
def minmax(a,b):
|
||||
if a < b:
|
||||
return a, b
|
||||
return b, a
|
||||
|
||||
def draw(path):
|
||||
for p1, p2 in zip(path, path[1:]):
|
||||
#print(p1, p2)
|
||||
if p1[0] == p2[0]:
|
||||
x = p1[0]
|
||||
y0, y1 = p1[1], p2[1]
|
||||
if y0 > y1:
|
||||
y0, y1 = y1, y0
|
||||
for y in range(y0, y1+1):
|
||||
put(x,y, '#')
|
||||
else:
|
||||
y = p1[1]
|
||||
x0, x1 = p1[0], p2[0]
|
||||
if x0 > x1:
|
||||
x0, x1 = x1, x0
|
||||
for x in range(x0, x1+1):
|
||||
put(x,y, '#')
|
||||
for (x0,y0), (x1,y1) in zip(path, path[1:]):
|
||||
#print((x0,y0), (x1,y1))
|
||||
x0, x1 = minmax(x0, x1)
|
||||
y0, y1 = minmax(y0, y1)
|
||||
for y in range(y0, y1+1):
|
||||
put(x0,y, '#')
|
||||
for x in range(x0, x1+1):
|
||||
put(x,y0, '#')
|
||||
|
||||
for path in data:
|
||||
#print(path)
|
||||
|
@ -69,53 +58,48 @@ def drip(p, floor=None, lastpath=[]):
|
|||
x,y=p
|
||||
if p in lastpath:
|
||||
x,y = lastpath.pop()
|
||||
while True:
|
||||
else:
|
||||
lastpath[:] = []
|
||||
blocked = False
|
||||
while not blocked:
|
||||
lastpath.append((x,y))
|
||||
if y+1 == floor:
|
||||
# blocked
|
||||
lastpath.pop()
|
||||
put(x,y,'o')
|
||||
return 'done'
|
||||
break
|
||||
if y >= Y1:
|
||||
return 'bye'
|
||||
c = get(x,y+1)
|
||||
if c == '.':
|
||||
y += 1
|
||||
continue
|
||||
# try left
|
||||
c = get(x-1,y+1)
|
||||
if c == '.':
|
||||
x,y = x-1,y+1
|
||||
continue
|
||||
# try right
|
||||
c = get(x+1, y+1)
|
||||
if c == '.':
|
||||
x,y = x+1, y+1
|
||||
continue
|
||||
# blocked!
|
||||
lastpath.pop()
|
||||
put(x,y,'o')
|
||||
#print(x,y)
|
||||
return 'done'
|
||||
return 'bye' # falls forever
|
||||
# try straight down, then left, then right
|
||||
for x1 in x, x-1, x+1:
|
||||
if get(x1,y+1) == '.':
|
||||
x,y = x1,y+1
|
||||
break # continue
|
||||
else:
|
||||
blocked = True
|
||||
# blocked!
|
||||
lastpath.pop()
|
||||
put(x,y,'o')
|
||||
#print(x,y)
|
||||
return 'done'
|
||||
|
||||
i = 0
|
||||
n = 0
|
||||
while True:
|
||||
if drip(source) == 'bye':
|
||||
break
|
||||
i+= 1
|
||||
print(i)
|
||||
n+= 1
|
||||
print(n)
|
||||
|
||||
floor = Y1 + 1
|
||||
Y1 = floor+1
|
||||
show()
|
||||
|
||||
# keep going
|
||||
|
||||
while source not in grid:
|
||||
r = drip(source, floor=floor)
|
||||
i += 1
|
||||
if r == 'bye':
|
||||
if drip(source, floor=floor) == 'bye':
|
||||
print("whoops") # shouldn't happen
|
||||
break
|
||||
n += 1
|
||||
|
||||
# 28594
|
||||
|
||||
show()
|
||||
print(i)
|
||||
#show()
|
||||
print(n)
|
||||
|
|
Loading…
Reference in New Issue