b.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os, argparse, re
  2. def solve_task(filename):
  3. horizontal_position = 0
  4. depth = 0
  5. aim = 0
  6. match_string = r"(?P<command>[a-z]*) (?P<distance>\d*)"
  7. matcher = re.compile(match_string)
  8. with open(filename) as infile:
  9. for raw_line in infile:
  10. line = raw_line.rstrip()
  11. m = matcher.match(line)
  12. command = m.group('command')
  13. distance = int(m.group('distance'))
  14. match command:
  15. case 'forward':
  16. horizontal_position += distance
  17. depth += aim * distance
  18. case 'up':
  19. aim -= distance
  20. case 'down':
  21. aim += distance
  22. print("Current depth: " + str(depth))
  23. print("Current horizontal_position: " + str(horizontal_position))
  24. print("Product: " + str(depth * horizontal_position))
  25. os.system("pause")
  26. def parse_arguments():
  27. parser = argparse.ArgumentParser(description="Solves a part of advent of code.",epilog="Have a nice day!")
  28. parser.add_argument('filename', nargs='?', default="infile.txt", help='Input file')
  29. args = parser.parse_args()
  30. return args
  31. def main():
  32. args = parse_arguments()
  33. solve_task(args.filename)
  34. if __name__ == "__main__":
  35. main()