-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_02.py
52 lines (37 loc) · 1.1 KB
/
day_02.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
from typing import List, Tuple
def parse(data: List[str]) -> List[Tuple[str, int]]:
res = []
for row in data:
splitted = row.strip().split(" ")
res.append((splitted[0], int(splitted[1])))
return res
def position(inputs: List[Tuple[str, int]]) -> int:
forward = 0
depth = 0
for direction, step in inputs:
if direction == "forward":
forward += step
elif direction == "down":
depth += step
elif direction == "up":
depth -= step
return depth * forward
def aim(inputs: List[Tuple[str, int]]) -> int:
forward = 0
depth = 0
aim_pos = 0
for direction, step in inputs:
if direction == "forward":
forward += step
depth += aim_pos * step
elif direction == "down":
aim_pos += step
elif direction == "up":
aim_pos -= step
return depth * forward
if __name__ == "__main__":
with open("../inputs/day_02.txt") as infile:
data = infile.readlines()
parsed = parse(data)
print(position(parsed))
print(aim(parsed))