-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (46 loc) · 1.23 KB
/
main.py
File metadata and controls
57 lines (46 loc) · 1.23 KB
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
test_input = """forward 5
down 5
forward 8
up 3
down 8
forward 2
"""
def part1(input_str: str):
horizontal_position = 0
depth = 0
for line in input_str.splitlines():
instruction, value = line.split(" ")
if instruction == "forward":
horizontal_position += int(value)
elif instruction == "down":
depth += int(value)
elif instruction == "up":
depth -= int(value)
return horizontal_position * depth
def part2(input_str: str):
horizontal_position = 0
depth = 0
aim = 0
for line in input_str.splitlines():
instruction, value = line.split(" ")
if instruction == "forward":
horizontal_position += int(value)
if aim == 0:
pass
else:
depth += int(value) * aim
elif instruction == "down":
aim += int(value)
elif instruction == "up":
aim -= int(value)
return horizontal_position * depth
if __name__ == "__main__":
f = open("../inputs/2021-02.txt", mode="r").read()
res = part1(test_input)
assert res == 150
res = part1(f)
print(res)
res = part2(test_input)
assert res == 900
res = part2(f)
print(res)