summaryrefslogtreecommitdiff
path: root/2022/day14.py
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2022-12-14 06:51:10 +0100
committernekineki <nekineki@nekineki.net>2022-12-14 06:51:10 +0100
commita1327be483160c5c0ed7105cd4c0b97999cefc75 (patch)
tree549fda7ed2d0a5fc45d77a4c726475e835dae4ee /2022/day14.py
parentd5a479c44f28be5b57949fb855c5e16c9fa90a04 (diff)
day14
Diffstat (limited to '2022/day14.py')
-rwxr-xr-x2022/day14.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/2022/day14.py b/2022/day14.py
new file mode 100755
index 0000000..bbc415c
--- /dev/null
+++ b/2022/day14.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python3
+
+# import numpy as np
+from functools import reduce
+from re import findall
+from copy import deepcopy
+import sys
+
+# filename = "in/day14.ref"
+filename = "in/day14.pzl"
+data = open(filename).read()
+lines = [line for line in data.rstrip('\n').split('\n')]
+
+G = set()
+
+for line in lines:
+ for a,b in zip(line.split(' -> '), line.split(' -> ')[1:]):
+ xs,ys = [int(i) for i in a.split(',')]
+ xe,ye = [int(i) for i in b.split(',')]
+ if xe < xs:
+ xs, xe = xe, xs
+ if ye < ys:
+ ys, ye = ye, ys
+ for x in range(xs, xe+1):
+ for y in range(ys, ye+1):
+ G.add((y,x))
+
+max_y = 0
+for y,x in G:
+ max_y = max(max_y, y)
+# print('max_y', max_y)
+
+def add_sand(G, part, y=0, x=500):
+ if part == 1 and y > max_y:
+ return G
+ elif part == 2 and y == max_y + 1:
+ G.add((y,x))
+ return G
+ elif (y+1, x) not in G:
+ return add_sand(G, part, y+1, x)
+ elif (y+1, x-1) not in G:
+ return add_sand(G, part, y+1, x-1)
+ elif (y+1, x+1) not in G:
+ return add_sand(G, part, y+1, x+1)
+ elif (y,x) not in G:
+ G.add((y,x))
+ return G
+ return G
+
+start_len = len(G)
+
+for part in [1,2]:
+ Gp = G.copy()
+ while True:
+ prev_len = len(Gp)
+ Gp = add_sand(Gp, part)
+ if len(Gp) == prev_len:
+ break
+ print(len(Gp) - start_len)
+