Untitled
unknown
plain_text
a year ago
1.9 kB
9
Indexable
f = open("./input", "r")
lines = f.readlines()
lines = [l.replace("\n", "") for l in lines]
ground = {}
max_depth = 0
sand_counter = 0
def form_rocks():
global max_depth
for paths in lines:
coords = paths.split(" -> ")
for i in range(len(coords)):
[x, y] = coords[i].split(",")
x = int(x)
y = int(y)
if y > max_depth:
max_depth = y
if i + 1 < len(coords):
[next_x, next_y] = coords[i + 1].split(",")
next_x = int(next_x)
next_y = int(next_y)
if next_y > max_depth:
max_depth = next_y
if x == next_x:
if y < next_y:
y_range = range(y, next_y + 1)
else:
y_range = range(next_y, y + 1)
for i in y_range:
ground[x, i] = 1
elif y == next_y:
if x < next_x:
x_range = range(x, next_x + 1)
else:
x_range = range(next_x, x + 1)
for i in x_range:
ground[i, y] = 1
def simulate_sand():
global sand_counter
sand_counter += 1
sand = {"x": 500, "y": 0}
while sand["y"] <= max_depth:
if ground[sand["x"], sand["y"]]:
if not ground[sand["x"] - 1, sand["y"] + 1]:
sand["x"] -= 1
sand["y"] += 1
elif not ground[sand["x"] + 1, sand["y"] + 1]:
sand["x"] += 1
sand["y"] += 1
else:
ground[sand["x"], sand["y"] - 1] = 1
simulate_sand()
else:
sand["y"] += 1
if sand["y"] > max_depth:
print("DONE", sand_counter)
form_rocks()
simulate_sand()
# find max depth
Editor is loading...
Leave a Comment