1
0

main.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os, sys
  2. from collections import defaultdict
  3. from typing import Callable
  4. task_dir = os.path.dirname(__file__)
  5. sys.path.append(f"{task_dir}/..")
  6. from get_tasks import get_input
  7. input, example = get_input(task_dir, 2)
  8. def check_example(example: list[str], part: Callable):
  9. part(example)
  10. def part1(input: list[str]):
  11. path: defaultdict = defaultdict(int)
  12. for go in input:
  13. direction, value = go.split()
  14. if direction == "forward":
  15. path["forward"] += int(value)
  16. if direction == "down":
  17. path["depth"] += int(value)
  18. if direction == "up":
  19. path["depth"] -= int(value)
  20. print("The answer of part1 is:", path["forward"] * path["depth"])
  21. def part2(input: list[str]):
  22. path = defaultdict(int)
  23. path["aim"] = 0
  24. for go in input:
  25. direction, value = go.split()
  26. if direction == "forward":
  27. path["forward"] += int(value)
  28. path["depth"] += int(value) * path["aim"]
  29. if direction == "down":
  30. path["aim"] += int(value)
  31. if direction == "up":
  32. path["aim"] -= int(value)
  33. print("The answer of part1 is:", path["forward"] * path["depth"])
  34. if __name__ == "__main__":
  35. check_example(example, part1)
  36. part1(input)
  37. check_example(example, part2)
  38. part2(input)