Skip to content

Commit 7ab4578

Browse files
committed
Add solution to 2024-12-14
1 parent 6e3275e commit 7ab4578

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

2024/day14/solutions.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from itertools import count
2+
from math import prod
3+
import re
4+
5+
import numpy as np
6+
7+
with open("input") as f:
8+
ns = [list(map(int, re.findall("-?\\d+", x))) for x in f.read().strip().split("\n")]
9+
10+
# Part 1
11+
w = 101
12+
h = 103
13+
qs = [0, 0, 0, 0]
14+
for px, py, vx, vy in ns:
15+
px = (px + 100 * vx) % w
16+
py = (py + 100 * vy) % h
17+
if px != w // 2 and py != h // 2:
18+
qs[(px > w // 2) + 2 * (py > h // 2)] += 1
19+
print(prod(qs))
20+
21+
# Part 2
22+
zs = np.array([px + 1j * py for px, py, _, _ in ns])
23+
vs = np.array([vx + 1j * vy for _, _, vx, vy in ns])
24+
max_has_neighbour = 0
25+
26+
for t in count(1):
27+
zs = np.array([int(z.real) % w + (int(z.imag) % h) * 1j for z in zs + vs])
28+
zs_set = set(zs)
29+
num_has_neighbour = sum(z + dz in zs_set for z in zs for dz in (1, -1, 1j, -1j))
30+
if num_has_neighbour > max_has_neighbour:
31+
max_has_neighbour = num_has_neighbour
32+
a = np.zeros((h, w), dtype=int)
33+
a[zs.imag.astype(int), zs.real.astype(int)] = 1
34+
print(t)
35+
print("\n".join("".join(" ■"[x] for x in row) for row in a))
36+
print()

0 commit comments

Comments
 (0)