-
Notifications
You must be signed in to change notification settings - Fork 0
/
a02.py
58 lines (54 loc) · 1.12 KB
/
a02.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
54
55
56
57
58
f = open("input/02.input")
example = """forward 5
down 5
forward 8
up 3
down 8
forward 2
"""
text = f.read()
def parse(t):
res = []
for line in t.strip().split('\n'):
move, num = line.strip().split()
num = int(num)
res.append((move, num))
return res
def part1(text):
moves = parse(text)
pos = 0
depth = 0
for (move, num) in moves:
if move == "forward":
pos += num
elif move == "down":
depth += num
elif move == "up":
depth -= num
else:
print("UNEXPECTED", move)
return
return pos * depth
def part2(text):
moves = parse(text)
pos = 0
depth = 0
aim = 0
for (move, num) in moves:
if move == "forward":
pos += num
depth += aim * num
elif move == "down":
aim += num
elif move == "up":
aim -= num
else:
print("UNEXPECTED", move)
return
return depth * pos
def main(text):
print("Part1:", part1(text))
print("Part2:", part2(text))
main(example)
print()
main(text)