-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_one.py
53 lines (36 loc) · 1.04 KB
/
part_one.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
from typing import override
from infrastructure.solutions.base import Solution
MOVE = tuple[str, int]
class Year2016Day1Part1Solution(Solution):
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, list[MOVE]]:
moves = []
for line in text_input.split(', '):
# R2 or L43 formats
direction = line[0]
steps = int(line[1:])
moves.append((direction, int(steps)))
return {'moves': moves}
@classmethod
@override
def solve(cls, moves: list[MOVE]) -> int:
"""
Time: O(n)
Space: O(1)
Where n - number of moves
"""
x = 0
y = 0
dx = 0
dy = 1
for direction, steps in moves:
if direction == 'L':
dx, dy = -dy, dx
if direction == 'R':
dx, dy = dy, -dx
x += dx * steps
y += dy * steps
return abs(x) + abs(y)
if __name__ == '__main__':
print(Year2016Day1Part1Solution.main())