-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14.py
More file actions
57 lines (44 loc) · 1.35 KB
/
day14.py
File metadata and controls
57 lines (44 loc) · 1.35 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import re
from math import prod
from lib.input import get_input
from lib.map import Position
WIDTH = 101
HEIGHT = 103
PATTERN = re.compile(r"p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)")
class Robot:
def __init__(self, pos, vel):
self.pos = pos
self.vel = vel
def move(self):
self.pos += self.vel
self.pos.x %= WIDTH
self.pos.y %= HEIGHT
@property
def cadran(self):
if self.pos.x < WIDTH // 2:
return 0 if self.pos.y < HEIGHT // 2 else 2 if self.pos.y > HEIGHT // 2 else 4
elif self.pos.x > WIDTH // 2:
return 1 if self.pos.y < HEIGHT // 2 else 3 if self.pos.y > HEIGHT // 2 else 4
return 4
lines = get_input(2024, 14).splitlines()
robots = set()
for line in lines:
numbers = [int(x) for x in PATTERN.match(line).groups()]
pos = Position(numbers[0], numbers[1])
vel = Position(numbers[2], numbers[3])
robots.add(Robot(pos, vel))
def part1():
res = [0] * 5 # 4 cadran + ignored middles ones
for _ in range(100):
for robot in robots:
robot.move()
for robot in robots:
res[robot.cadran] += 1
return prod(res[:4])
def part2():
for i in range(10000):
for robot in robots:
robot.move()
if len({robot.pos for robot in robots}) == 500:
break
return i + 1 + 100 # part1 offset