part2.py 539 B

1234567891011121314151617181920212223242526
  1. import argparse
  2. parser = argparse.ArgumentParser()
  3. parser.add_argument("ifile", type=argparse.FileType('r'))
  4. args = parser.parse_args()
  5. lines = [line.strip() for line in args.ifile.readlines()]
  6. hpos = 0
  7. depth = 0
  8. aim = 0
  9. for line in lines:
  10. stuff = line.split(' ')
  11. command = stuff[0]
  12. parameter = int(stuff[1])
  13. if command == "forward":
  14. hpos += parameter
  15. depth += parameter * aim
  16. elif command == "down":
  17. aim += parameter
  18. elif command == "up":
  19. aim -= parameter
  20. print(hpos * depth)