a_ugly.py 894 B

12345678910111213141516171819202122232425262728
  1. import os, argparse, re
  2. def solve_task(filename):
  3. horizontal_position = 0
  4. depth = 0
  5. with open(filename) as infile:
  6. for line in infile:
  7. distance = int(re.search(r"(?P<command>[a-z]*) (?P<distance>\d*)", line).group('distance'))
  8. horizontal_position += distance if 'forward' in line else 0
  9. depth += distance if 'down' in line else -distance if 'up' in line else 0
  10. print("Product: " + str(depth * horizontal_position))
  11. os.system("pause")
  12. def parse_arguments():
  13. parser = argparse.ArgumentParser(description="Solves a part of advent of code.",epilog="Have a nice day!")
  14. parser.add_argument('filename', nargs='?', default="infile.txt", help='Input file')
  15. args = parser.parse_args()
  16. return args
  17. def main():
  18. args = parse_arguments()
  19. solve_task(args.filename)
  20. if __name__ == "__main__":
  21. main()