summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2024/14/python/main.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/2024/14/python/main.py b/2024/14/python/main.py
new file mode 100644
index 0000000..ebfe29c
--- /dev/null
+++ b/2024/14/python/main.py
@@ -0,0 +1,62 @@
+import re
+from fileinput import input
+from itertools import groupby
+from math import prod
+
+XMAX, YMAX = 11, 7 # for test
+XMAX, YMAX = 101, 103 # for aoc
+
+robots = [
+ re.fullmatch(r"p=(\d+),(\d+) v=(-?\d+),(-?\d+)", line.strip()).groups()
+ for line in input()
+]
+robots = [
+ (complex(int(px), int(py)), complex(int(vx), int(vy))) for px, py, vx, vy in robots
+]
+
+
+def move(p, v, s):
+ e = p + v * s
+ return complex(e.real % XMAX, e.imag % YMAX)
+
+
+def quad(x):
+ return (x.real < XMAX // 2, x.imag < YMAX // 2)
+
+
+def is_quad(x):
+ return x.real != XMAX // 2 and x.imag != YMAX // 2
+
+
+def draw(s):
+ print(f"Seconds passed: {s}")
+ moved = {move(p, v, s) for p, v in robots}
+ for x in range(XMAX):
+ for y in range(YMAX):
+ print(
+ "#" if complex(x, y) in moved else " ",
+ end="",
+ )
+ print()
+ print("-" * XMAX)
+
+
+moved = groupby(
+ sorted(
+ filter(is_quad, (move(p, v, 100) for p, v in robots)),
+ key=quad,
+ ),
+ quad,
+)
+
+
+silver = prod(sum(1 for _ in k) for _, k in moved)
+gold = 0
+
+print("silver:", silver)
+print("gold:", gold)
+
+s = 0
+while s < 10_000:
+ draw(s)
+ s += 1