Skip to content

Commit 823e1d7

Browse files
committed
Add solution to 2024-12-14
1 parent 6e3275e commit 823e1d7

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

2024/day14/solutions.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
for _ in range(100):
16+
px += vx
17+
py += vy
18+
px %= w
19+
py %= h
20+
if px != w // 2 and py != h // 2:
21+
qs[(px > w // 2) + 2 * (py > h // 2)] += 1
22+
print(prod(qs))
23+
24+
# Part 2
25+
zs = np.array([px + 1j * py for px, py, _, _ in ns])
26+
vs = np.array([vx + 1j * vy for _, _, vx, vy in ns])
27+
max_has_neighbour = 0
28+
29+
for t in count(1):
30+
zs = np.array([int(z.real) % w + (int(z.imag) % h) * 1j for z in zs + vs])
31+
zs_set = set(zs)
32+
num_has_neighbour = sum(z + dz in zs_set for z in zs for dz in (1, -1, 1j, -1j))
33+
if num_has_neighbour > max_has_neighbour:
34+
max_has_neighbour = num_has_neighbour
35+
a = np.zeros((h, w), dtype=int)
36+
a[zs.imag.astype(int), zs.real.astype(int)] = 1
37+
print(t)
38+
print("\n".join("".join(" ■"[x] for x in row) for row in a))
39+
print()

0 commit comments

Comments
 (0)