Skip to content

Commit

Permalink
Add solution to 2024-12-14
Browse files Browse the repository at this point in the history
  • Loading branch information
fuglede committed Dec 14, 2024
1 parent 6e3275e commit 7ab4578
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 2024/day14/solutions.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7ab4578

Please sign in to comment.