-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.py
More file actions
28 lines (25 loc) · 719 Bytes
/
day02.py
File metadata and controls
28 lines (25 loc) · 719 Bytes
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
# AOC 2021 Day 02
def decode_instruction(x):
if x[0] == "forward":
return [0, int(x[1])]
elif x[0] == "up":
return [-1, int(x[1])]
elif x[0] == "down":
return [1, int(x[1])]
else: # invalid
return [0, 0]
with open('day02.input') as f:
input = [x.split() for x in f.readlines()]
# Part 1
h = sum(i[1] for i in [decode_instruction(x) for x in input] if i[0] == 0)
d = sum(i[0] * i[1] for i in [decode_instruction(x) for x in input])
print(h*d)
# Part 2
h = sum(i[1] for i in [decode_instruction(x) for x in input] if i[0] == 0)
d = 0
aim = 0
for i in [decode_instruction(x) for x in input]:
aim += i[0] * i[1]
if i[0] == 0:
d += aim * i[1]
print(h*d)