Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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