-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday14.py
34 lines (24 loc) · 1021 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
28
29
30
31
32
33
34
from math import prod
import re
data = open(0).read().splitlines()
inp = {i: tuple(map(int, re.findall(r"-?\d+", line))) for i, line in enumerate(data)}
xm = (w := 11 if len(inp) <= 12 else 101) // 2
ym = (h := 7 if len(inp) <= 12 else 103) // 2
def part1():
rs = [((px + vx * 100) % w, (py + vy * 100) % h) for px, py, vx, vy in inp.values()]
ans = [0] * 4
for px, py in rs: # Count each of the quadrants
ans[0] += px > xm and py > ym
ans[1] += px < xm and py > ym
ans[2] += px < xm and py < ym
ans[3] += px > xm and py < ym
return prod(ans)
def part2():
c = lambda p: abs(p[0] - xm) <= 20 and abs(p[1] - ym) <= 20 # Count hits in center
for k in range(10000): # 100 seconds have already passed in Part 1
if sum(map(c, inp.values())) > 290: # Good threshold to detect the easter egg
return k
for i, (px, py, vx, vy) in inp.items():
inp[i] = ((px + vx) % w, (py + vy) % h, vx, vy)
print(part1())
print(part2())