-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
27 lines (23 loc) · 892 Bytes
/
day14.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import re
from math import prod
from collections import defaultdict
robots = [list(map(int, re.findall(r'-?\d+', line))) for line in open('input14.txt').readlines()]
def move(robot, width, height, seconds=1):
pos = complex(robot[0], robot[1])
vel = complex(robot[2], robot[3])
pos = pos + vel * seconds
x, y = int(pos.real)%width, int(pos.imag)%height
midX, midY = width//2, height//2
if x != midX and y != midY: quadrants[(x-midX > 0, y-midY > 0)] += 1
if seconds == 1: robot[0], robot[1] = x, y
# Part 1
quadrants = defaultdict(int)
list(move(robot, width=101, height=103, seconds=100) for robot in robots)
print(prod(quadrants.values()))
# Part 2
seconds = 0
while seconds := seconds + 1:
quadrants = defaultdict(int)
list(move(robot, width=101, height=103) for robot in robots)
if any(v > 230 for v in quadrants.values()): break
print(seconds)