summary refs log tree commit diff
path: root/22.14/main.py
diff options
context:
space:
mode:
authordzwdz2022-12-14 15:09:27 +0100
committerdzwdz2022-12-14 15:09:27 +0100
commit8c1c438930eb450102f992fbb88d777c9476cbf4 (patch)
tree378009a14014a0b7e0f6bb80de11a8561daea069 /22.14/main.py
parenta8a2b4608a171fcb77623559ba946daeec2699d8 (diff)
day 14 part 1
Diffstat (limited to '22.14/main.py')
-rw-r--r--22.14/main.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/22.14/main.py b/22.14/main.py
new file mode 100644
index 0000000..5078f4d
--- /dev/null
+++ b/22.14/main.py
@@ -0,0 +1,38 @@
+from sys import stdin
+
+def birange(a, b):
+	if b < a: a, b = b, a
+	return range(a, b + 1)
+
+solid = set()
+for line in stdin:
+	parts = [[int(n) for n in s.split(',')] for s in line.split("->")]
+	for a, b in zip(parts, parts[1:]):
+		for x in birange(a[0], b[0]):
+			for y in birange(a[1], b[1]):
+				solid.add((x, y))
+
+max_y = max([y for x,y in solid])
+print(max_y)
+
+def drop(x, y):
+	while True:
+		while not (x, y+1) in solid:
+			y += 1
+			if max_y < y:
+				return False
+		if not (x-1, y+1) in solid:
+			x -= 1
+			y += 1
+		elif not (x+1, y+1) in solid:
+			x += 1
+			y += 1
+		else:
+			print(x, y)
+			solid.add((x, y))
+			return True
+
+n = 0
+while drop(500, 0):
+	n += 1
+print(n)