part1.py 501 B

123456789101112131415161718192021222324
  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. for line in lines:
  9. stuff = line.split(' ')
  10. command = stuff[0]
  11. parameter = int(stuff[1])
  12. if command == "forward":
  13. hpos += parameter
  14. elif command == "down":
  15. depth += parameter
  16. elif command == "up":
  17. depth -= parameter
  18. print(hpos * depth)