Day 14
This commit is contained in:
62
day14/part1.py
Normal file
62
day14/part1.py
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
import math
|
||||
from functools import reduce
|
||||
from itertools import count
|
||||
|
||||
import numpy as np
|
||||
|
||||
lines = list(np.array([[int(z) for z in y.split(',')] for y in x.strip().split(" -> ")]) for x in open("input.txt"))
|
||||
|
||||
|
||||
all = reduce(lambda x, y: np.concatenate((x, y)), lines, np.array([[500, 0]]))
|
||||
span = np.zeros((2, 2), dtype=int)
|
||||
span[0] = np.min(all, axis=0)
|
||||
span[1] = np.max(all, axis=0) + 1
|
||||
print(span)
|
||||
grid = np.full(tuple(span[1]-span[0]), False)
|
||||
|
||||
for line in lines:
|
||||
line = line-span[0]
|
||||
#print("line", line)
|
||||
for a, b in zip(line[:-1], line[1:]):
|
||||
a, b = np.min((a, b), axis=0), np.max((a, b), axis=0)
|
||||
#print("pair", a, b)
|
||||
grid[a[0]:b[0]+1, a[1]:b[1]+1] = True
|
||||
#print('\n+'.join(''.join('#' if p else '.' for p in l) for l in grid.T))
|
||||
|
||||
def drop():
|
||||
x, y = 500-span[0, 0], 0
|
||||
if grid[y, x]:
|
||||
return False
|
||||
while True:
|
||||
#print("Test", x-span[0, 0], y)
|
||||
if y+1 == grid.shape[1]:
|
||||
return None
|
||||
elif not grid[x, y+1]:
|
||||
y += 1
|
||||
elif x == 0:
|
||||
return None
|
||||
elif not grid[x-1, y+1]:
|
||||
x, y = x-1, y+1
|
||||
elif x+1 == grid.shape[0]:
|
||||
return None
|
||||
elif not grid[x+1, y+1]:
|
||||
x, y = x+1, y+1
|
||||
else:
|
||||
return x, y
|
||||
|
||||
|
||||
for cnt in count():
|
||||
pos = drop()
|
||||
#print(pos)
|
||||
|
||||
if pos is None:
|
||||
print("Res", cnt)
|
||||
break
|
||||
if not pos:
|
||||
print("Start is occupied!")
|
||||
break
|
||||
grid[pos] = True
|
||||
print('\n'.join(''.join('#' if p else '.' for p in l) for l in grid.T))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user