advent-of-code/2024/14/one.py

89 lines
1.6 KiB
Python
Raw Normal View History

2024-12-25 12:58:02 +01:00
#!/usr/bin/env python3
import functools
import re
import sys
input_file = sys.argv[1]
with open(input_file) as fd:
lines = [line.rstrip() for line in fd.readlines()]
vec = tuple[int, int]
poss: list[vec] = list()
vits: list[vec] = list()
for line in lines:
match = re.findall(r"(-?\d+)", line)
assert match
pos = int(match[0]), int(match[1])
vit = int(match[2]), int(match[3])
poss.append(pos)
vits.append(vit)
print(poss, vits)
def print_poss(poss: list[vec]) -> None:
viz = [[0] * width for _ in range(height)]
for pos in poss:
px, py = pos
viz[py][px] += 1
for line in viz:
print("".join([(str(c) if c > 0 else ".") for c in line]))
print()
# x→ y↓
if input_file == "input":
width = 101
height = 103
else:
width = 11
height = 7
if input_file == "demo1":
secs = 5
else:
secs = 100
print_poss(poss)
for s in range(secs):
for r, vit in enumerate(vits):
px, py = poss[r]
px += vit[0]
py += vit[1]
while px >= width:
px -= width
while py >= height:
py -= height
while px < 0:
px += width
while py < 0:
py += height
poss[r] = px, py
print(s)
print_poss(poss)
half_width = width // 2
half_height = height // 2
# <<<<<|>>>>>
# <= first quadrant
quadrants = [0, 0, 0, 0]
for pos in poss:
px, py = pos
q = 0
if px == half_width or py == half_height:
continue
if px > half_width:
q += 1
if py > half_height:
q += 2
quadrants[q] += 1
print(quadrants)
print(functools.reduce(int.__mul__, quadrants))