-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_01.py
61 lines (49 loc) · 2.28 KB
/
task_01.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Standard Library
import math
import re
# From apps
from utils.point import Point
class Task01:
@classmethod
def solve(cls, width: int, height: int, file_content: list[str]) -> int:
pattern = re.compile("p=(-?\\d+),(-?\\d+) v=(-?\\d+),(-?\\d+)")
robots = []
for line in file_content:
mul_instructions = pattern.findall(line)
for instruction in mul_instructions:
position = Point(int(instruction[0]), int(instruction[1]))
velocity = Point(int(instruction[2]), int(instruction[3]))
robots.append((position, velocity))
for count in range(100):
robots = cls.move_robots(width=width, height=height, robots=robots)
return cls.calculate_safety_factor(width=width, height=height, robots=robots)
@classmethod
def move_robots(cls, width: int, height: int, robots: list[tuple[Point, Point]]) -> list[tuple[Point, Point]]:
new_positions = []
for robot in robots:
current_position = robot[0]
movement = robot[1]
new_x = (current_position.x + movement.x) % width
new_y = (current_position.y + movement.y) % height
new_position = Point(x=new_x, y=new_y)
new_positions.append((new_position, movement))
return new_positions
@classmethod
def calculate_safety_factor(cls, width: int, height: int, robots: list[tuple[Point, Point]]) -> int:
mid_x = math.floor(width / 2)
mid_y = math.floor(height / 2)
top_left = 0
top_right = 0
bottom_left = 0
bottom_right = 0
for robot in robots:
if robot[0].is_in_bounds(start_x=0, start_y=0, width=mid_x, height=mid_y):
top_left = top_left + 1
if robot[0].is_in_bounds(start_x=mid_x + 1, start_y=0, width=mid_x, height=mid_y):
top_right = top_right + 1
if robot[0].is_in_bounds(start_x=0, start_y=mid_y + 1, width=mid_x, height=mid_y):
bottom_left = bottom_left + 1
if robot[0].is_in_bounds(start_x=mid_x + 1, start_y=mid_y + 1, width=mid_x, height=mid_y):
bottom_right = bottom_right + 1
safety_factor = top_left * top_right * bottom_left * bottom_right
return safety_factor