-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
51 lines (40 loc) · 1.15 KB
/
day2.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
with open("./input2.txt") as file:
commands = []
for line in file:
command = line.split()
commands.append((command[0], int(command[1])))
def part_1(commands):
h_position = 0
depth = 0
for command in commands:
match command:
case "forward", by:
h_position += by
case "down", by:
depth += by
case "up", by:
depth -= by
return h_position, depth
def part_2(commands):
aim = 0
h_position = 0
depth = 0
for command in commands:
match command:
case "forward", by:
h_position += by
depth += aim * by
case "down", by:
aim += by
case "up", by:
aim -= by
return h_position, depth
if __name__ == "__main__":
h_position, depth = part_1(commands)
print(
f"Part 1\nHorizontal position: {h_position}, Depth: {depth}\nAnswer: {h_position * depth}"
)
h_position, depth = part_2(commands)
print(
f"Part 2\nHorizontal position: {h_position}, Depth: {depth}\nAnswer: {h_position * depth}"
)