You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aoc2-1.py 895B

3 jaren geleden
123456789101112131415161718192021222324252627
  1. def main():
  2. """
  3. horizontal position and vertical position both start at 0
  4. forward X increases forward (horizontal) position by X units
  5. down X increases depth (vertical position) by X units
  6. up X decreases depth (vertical position) by X units
  7. After following all instructions multiply final h_pos by final v_pos
  8. """
  9. h_pos, v_pos = 0, 0
  10. instructions = []
  11. with open("aoc2-1.txt", "r") as file:
  12. instructions = file.readlines()
  13. for inst in instructions:
  14. dir, amt = inst.strip().split(" ")
  15. amt = int(amt)
  16. if dir == "forward":
  17. h_pos += amt
  18. elif dir == "down":
  19. v_pos += amt
  20. elif dir == "up":
  21. v_pos -= amt
  22. else:
  23. raise ValueError(f"Unrecognized direction: {h_pos}")
  24. return (h_pos * v_pos)
  25. if __name__ == "__main__":
  26. print(main())