-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
41 lines (36 loc) · 1.07 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
import sys
def part1(scan):
position, depth = 0, 0
for command in scan:
direction, amount = command.strip().split()
number = int(amount)
if direction == "forward":
position += number
elif direction == "down":
depth -= number
elif direction == "up":
depth += number
return (position, depth)
def part2(scan):
position, depth, aim = 0, 0, 0
for command in scan:
direction, amount = command.strip().split()
number = int(amount)
if direction == "forward":
position += number
depth += aim * number
elif direction == "down":
aim -= number
elif direction == "up":
aim += number
return (position, depth)
def main(args = ()):
arg = "day2.txt" if len(args) < 1 else args[0]
with open(arg, "r") as scan:
x, y = part1(scan)
print("Part 1:", abs(x*y))
scan.seek(0)
x, y = part2(scan)
print("Part 2:", abs(x*y))
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))