a.py 1.2 KB

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