You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
890 B
44 lines
890 B
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
|
|
if __name__ == '__main__':
|
|
lines = open(sys.argv[1]).readlines()
|
|
dirs = [i.strip('\n').split(" ") for i in lines]
|
|
|
|
# challenge 1
|
|
dep = 0
|
|
pos = 0
|
|
|
|
for d in dirs:
|
|
if d[0] == 'forward':
|
|
pos += int(d[1])
|
|
elif d[0] == 'up':
|
|
dep -= int(d[1])
|
|
elif d[0] == 'down':
|
|
dep += int(d[1])
|
|
else:
|
|
print(d)
|
|
|
|
res1 = str(dep * pos)
|
|
print("challenge 1:" + res1 + "\n")
|
|
|
|
# challenge 2
|
|
dep = 0
|
|
pos = 0
|
|
aim = 0
|
|
|
|
for d in dirs:
|
|
if d[0] == 'forward':
|
|
pos += int(d[1])
|
|
dep += int(d[1]) * aim
|
|
elif d[0] == 'up':
|
|
aim -= int(d[1])
|
|
elif d[0] == 'down':
|
|
aim += int(d[1])
|
|
else:
|
|
print(d)
|
|
res2 = str(dep * pos)
|
|
print("challenge 2:" + res2 + "\n")
|
|
|
|
|