-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_two.py
55 lines (39 loc) · 1.25 KB
/
part_two.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
from enum import StrEnum
from typing import override
from infrastructure.solutions.base import Solution
class Direction(StrEnum):
FORWARD = 'forward'
DOWN = 'down'
UP = 'up'
class Year2021Day2Part2Solution(Solution):
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, list[tuple[Direction, int]]]:
course = []
for line in text_input.split('\n'):
direction, distance = line.split()
direction = Direction(direction)
distance = int(distance)
course.append((direction, distance))
return {'course': course}
@classmethod
@override
def solve(cls, course: list[tuple[Direction, int]]) -> int:
"""
Time: O(n)
Space: O(1)
Where n - number of instructions in course
"""
x = y = z = 0
for direction, distance in course:
match direction:
case Direction.FORWARD:
x += distance
y += distance * z
case Direction.DOWN:
z += distance
case Direction.UP:
z -= distance
return x * y
if __name__ == '__main__':
print(Year2021Day2Part2Solution.main())