Removed extraneous stuff from 2018 Day 17
This commit is contained in:
parent
79befca869
commit
2c1f86898e
|
@ -3,16 +3,11 @@ with open('day17-input', 'r') as file:
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import re
|
import re
|
||||||
|
|
||||||
# numbers = [[int(s) for s in re.findall(r'-?\d+', d)] for d in data]
|
|
||||||
Xmin = [int(re.findall(r'x=(\d+)', d)[0]) for d in data]
|
Xmin = [int(re.findall(r'x=(\d+)', d)[0]) for d in data]
|
||||||
Xmax = [int(re.findall(r'x=(?:\d+\.\.)?(\d+)', d)[0]) for d in data]
|
Xmax = [int(re.findall(r'x=(?:\d+\.\.)?(\d+)', d)[0]) for d in data]
|
||||||
Ymin = [int(re.findall(r'y=(\d+)', d)[0]) for d in data]
|
Ymin = [int(re.findall(r'y=(\d+)', d)[0]) for d in data]
|
||||||
Ymax = [int(re.findall(r'y=(?:\d+\.\.)?(\d+)', d)[0]) for d in data]
|
Ymax = [int(re.findall(r'y=(?:\d+\.\.)?(\d+)', d)[0]) for d in data]
|
||||||
|
|
||||||
# arr = np.array(numbers, dtype=np.int64)
|
|
||||||
#
|
|
||||||
# # x, y_start, y_end
|
|
||||||
#
|
|
||||||
y_min = min(Ymin)
|
y_min = min(Ymin)
|
||||||
y_max = max(Ymax)
|
y_max = max(Ymax)
|
||||||
|
|
||||||
|
@ -20,30 +15,30 @@ x_min = min(Xmin) - 1
|
||||||
x_max = 700 - x_min
|
x_max = 700 - x_min
|
||||||
|
|
||||||
init_grid = np.zeros((x_max, y_max+1), dtype=np.int64) # 0 = Sand, 1 = Clay, 2 = water, -1 = running water
|
init_grid = np.zeros((x_max, y_max+1), dtype=np.int64) # 0 = Sand, 1 = Clay, 2 = water, -1 = running water
|
||||||
spring = [500-x_min, 0]
|
spring = (500-x_min, 0)
|
||||||
|
|
||||||
for xmin, xmax, ymin, ymax in zip(Xmin, Xmax, Ymin, Ymax):
|
for xmin, xmax, ymin, ymax in zip(Xmin, Xmax, Ymin, Ymax):
|
||||||
init_grid[xmin-x_min:xmax+1-x_min, ymin:ymax+1] = 1
|
init_grid[xmin-x_min:xmax+1-x_min, ymin:ymax+1] = 1
|
||||||
|
|
||||||
|
|
||||||
grid = init_grid.copy()
|
grid = init_grid.copy()
|
||||||
drop_point_blacklist = set()
|
drop_point_blacklist = set() # Cliff drop-offs that are guaranteed to go nowhere
|
||||||
|
|
||||||
|
|
||||||
def drop_water(source=spring, skip_cliffs=set()):
|
def drop_water(source=spring):
|
||||||
if tuple(source) in drop_point_blacklist:
|
if tuple(source) in drop_point_blacklist:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
x, y = source
|
x, y = source
|
||||||
# print('Dropping water from', source)
|
|
||||||
surface = (grid[x, y:] > 0).argmax()
|
surface = (grid[x, y:] > 0).argmax()
|
||||||
if surface == 0:
|
if surface == 0:
|
||||||
if (grid[x, y-1:] > 0).argmax() == 0:
|
if (grid[x, y-1:] > 0).argmax() == 0:
|
||||||
grid[x, y:] = -1
|
grid[x, y:] = -1
|
||||||
drop_point_blacklist.add(tuple(source))
|
drop_point_blacklist.add(tuple(source))
|
||||||
return False
|
return False
|
||||||
# raise ValueError('Out of bounds')
|
|
||||||
surface_y = surface + y
|
surface_y = surface + y
|
||||||
grid[x, y:surface_y] = -1
|
grid[x, y:surface_y] = -1
|
||||||
|
|
||||||
# Find walls left and right
|
# Find walls left and right
|
||||||
wall_left = (grid[x:0:-1, surface_y-1] > 0).argmax()
|
wall_left = (grid[x:0:-1, surface_y-1] > 0).argmax()
|
||||||
wall_right = (grid[x:, surface_y-1] > 0).argmax()
|
wall_right = (grid[x:, surface_y-1] > 0).argmax()
|
||||||
|
@ -69,17 +64,17 @@ def drop_water(source=spring, skip_cliffs=set()):
|
||||||
grid[x-cliff:x, surface_y-1] = -1
|
grid[x-cliff:x, surface_y-1] = -1
|
||||||
if wall_right:
|
if wall_right:
|
||||||
grid[x:right_x, surface_y - 1] = -1
|
grid[x:right_x, surface_y - 1] = -1
|
||||||
if cliff and (cliff not in skip_cliffs):
|
if cliff:
|
||||||
cliffs.add((x - cliff, surface_y))
|
cliffs.add((x - cliff, surface_y))
|
||||||
if not wall_right:
|
if not wall_right:
|
||||||
cliff = int((grid[x:, surface_y] <= 0).argmax())
|
cliff = int((grid[x:, surface_y] <= 0).argmax())
|
||||||
grid[x:x+cliff+1, surface_y-1] = -1
|
grid[x:x+cliff+1, surface_y-1] = -1
|
||||||
if wall_left:
|
if wall_left:
|
||||||
grid[left_x:x, surface_y-1] = -1
|
grid[left_x:x, surface_y-1] = -1
|
||||||
if cliff and (cliff not in skip_cliffs):
|
if cliff:
|
||||||
cliffs.add((x + cliff, surface_y))
|
cliffs.add((x + cliff, surface_y))
|
||||||
|
|
||||||
if cliffs and len([c for c in cliffs if tuple(c) in drop_point_blacklist]) == len(cliffs):
|
if cliffs and len([c for c in cliffs if tuple(c) in drop_point_blacklist]) == len(cliffs): # We go to 1 or 2 cliffs, all of which are dead-ends. We are a dead end, stop looking at us.
|
||||||
drop_point_blacklist.add(tuple(source))
|
drop_point_blacklist.add(tuple(source))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -93,12 +88,9 @@ def visualize():
|
||||||
vis_rows = []
|
vis_rows = []
|
||||||
for row in range(y_max+1):
|
for row in range(y_max+1):
|
||||||
vis_rows.append(''.join([('.', '#', '~', '|')[grid[col, row]] for col in range(x_max)]))
|
vis_rows.append(''.join([('.', '#', '~', '|')[grid[col, row]] for col in range(x_max)]))
|
||||||
|
|
||||||
with open('day17-vis', 'w') as file:
|
with open('day17-vis', 'w') as file:
|
||||||
file.write('\n'.join(vis_rows))
|
file.write('\n'.join(vis_rows))
|
||||||
|
|
||||||
import sys
|
|
||||||
sys.setrecursionlimit(10000)
|
|
||||||
|
|
||||||
while drop_water():
|
while drop_water():
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in New Issue