From 7ab4578544b35b36e0d5c7b33bc39e23a071b627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Fuglede=20J=C3=B8rgensen?= Date: Sat, 14 Dec 2024 18:38:23 +0100 Subject: [PATCH] Add solution to 2024-12-14 --- 2024/day14/solutions.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2024/day14/solutions.py diff --git a/2024/day14/solutions.py b/2024/day14/solutions.py new file mode 100644 index 0000000..4ea3444 --- /dev/null +++ b/2024/day14/solutions.py @@ -0,0 +1,36 @@ +from itertools import count +from math import prod +import re + +import numpy as np + +with open("input") as f: + ns = [list(map(int, re.findall("-?\\d+", x))) for x in f.read().strip().split("\n")] + +# Part 1 +w = 101 +h = 103 +qs = [0, 0, 0, 0] +for px, py, vx, vy in ns: + px = (px + 100 * vx) % w + py = (py + 100 * vy) % h + if px != w // 2 and py != h // 2: + qs[(px > w // 2) + 2 * (py > h // 2)] += 1 +print(prod(qs)) + +# Part 2 +zs = np.array([px + 1j * py for px, py, _, _ in ns]) +vs = np.array([vx + 1j * vy for _, _, vx, vy in ns]) +max_has_neighbour = 0 + +for t in count(1): + zs = np.array([int(z.real) % w + (int(z.imag) % h) * 1j for z in zs + vs]) + zs_set = set(zs) + num_has_neighbour = sum(z + dz in zs_set for z in zs for dz in (1, -1, 1j, -1j)) + if num_has_neighbour > max_has_neighbour: + max_has_neighbour = num_has_neighbour + a = np.zeros((h, w), dtype=int) + a[zs.imag.astype(int), zs.real.astype(int)] = 1 + print(t) + print("\n".join("".join(" ■"[x] for x in row) for row in a)) + print()