Skip to content

Commit 3f78436

Browse files
committed
Add solution to 2024-12-14
1 parent 6e3275e commit 3f78436

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

2024/day14/solutions.py

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

0 commit comments

Comments
 (0)