1
0

day1.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import os
  2. with open(os.path.join(os.path.dirname(__file__), "example.txt")) as example:
  3. example_data = example.read().splitlines()
  4. with open(os.path.join(os.path.dirname(__file__), "input.txt")) as example:
  5. input_data = example.read().splitlines()
  6. dial = 50
  7. zero_times = 0
  8. def part1(data=example_data):
  9. dial = 50
  10. zero_times = 0
  11. for instruction in data:
  12. direction, amount = instruction[0], int(instruction[1:])
  13. if direction == "R":
  14. dial = (dial + amount) % 100
  15. elif direction == "L":
  16. dial = (dial - amount) % 100
  17. if dial == 0:
  18. zero_times += 1
  19. print(f"{dial=}, {zero_times=}")
  20. part1(example_data)
  21. part1(input_data)
  22. def part2(data=example_data):
  23. dial = 50
  24. zero_times = 0
  25. cross_zero_times = 0
  26. was_zero = False
  27. for instruction in data:
  28. direction, amount = instruction[0], int(instruction[1:])
  29. if amount > 100:
  30. cross_zero_times += amount // 100
  31. amount = amount % 100
  32. if direction == "R":
  33. dial = dial + amount
  34. if dial > 100:
  35. cross_zero_times += 1
  36. elif direction == "L":
  37. dial = dial - amount
  38. if dial < 0 and not was_zero:
  39. cross_zero_times += 1
  40. dial = dial % 100
  41. if dial == 0:
  42. zero_times += 1
  43. was_zero = True
  44. else:
  45. was_zero = False
  46. print(f"{dial=}, {zero_times=}, {cross_zero_times=}, {zero_times + cross_zero_times=}")
  47. part2(example_data)
  48. part2(input_data)