adventofcode2024/day14/sol.py

43 lines
970 B
Python

import re
def parse(file):
robots = []
for line in file:
match = re.findall(r'-?\d+', line)
px,py, vx,vy = [int(x) for x in match]
robots.append(((px,py),(vx,vy)))
return robots
def solve(file, W, H):
robots = parse(file)
print(robots)
robots = [iterate(r, 100, W, H) for r in robots]
print(robots)
mx = W//2
my = H//2
quads = {1:[], 2:[], 3:[], 4:[]}
print(mx,my)
for (x,y),_ in robots:
q = 0
if x < mx and y < my: q = 1
if x > mx and y < my: q = 2
if x < mx and y > my: q = 3
if x > mx and y > my: q = 4
if q > 0:
quads[q].append((x,y))
print(quads)
total = 1
for v in quads.values():
total *= len(v)
print(total)
def iterate(robot, t, W, H):
(px,py), (vx,vy) = robot
px = (px+vx*t)%W
py = (py+vy*t)%H
return (px,py), (vx,vy)
#solve(open("sample1.in"), 11, 7)
solve(open("input"), 101, 103)