Guest User

Untitled

a guest
Dec 13th, 2025
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. # Input: lines like "L<number>" or "R<number>"
  2. # Output: number starts at 50, L decreases, R increases
  3. #         range is 0 to 99 (out of bounds -> wrap around)
  4. #         how many times does it hit 0?
  5. #         part 1: at the end of a move / part 2: also mid-move
  6.  
  7. from tkinter import *
  8. import math
  9. import time
  10.  
  11. pointer_ids = []
  12. instruction_id = -1
  13. number_instructions_id = -1
  14. password_part1_id = -1
  15. password_part2_id = -1
  16.  
  17. old_pointer = -1
  18. number_hits_part1 = 0
  19. number_hits_part2 = 0
  20. number_instructions_so_far = 0
  21.  
  22. # *** graphics functions ***
  23.  
  24. def get_radians(index):
  25.   return math.tau * index / 100
  26.  
  27. def get_x(index, distance):
  28.   return int(250 + math.sin(get_radians(index)) * distance)
  29.  
  30. def get_y(index, distance):
  31.   return int(250 - math.cos(get_radians(index)) * distance)
  32.  
  33. def refresh_display():
  34.   root.update_idletasks()
  35.   root.update()
  36.  
  37. def draw_pointer():
  38.   global old_pointer # required to be able to alter it
  39.   if old_pointer >= 0:
  40.     canvas.itemconfigure(pointer_ids[old_pointer], state=HIDDEN)
  41.   old_pointer = pointer
  42.   canvas.itemconfigure(pointer_ids[pointer], state=NORMAL)
  43.   refresh_display()
  44.  
  45. def update_password():
  46.   canvas.itemconfigure(password_part1_id, text="Part 1: " + str(number_hits_part1))
  47.   canvas.itemconfigure(password_part2_id, text="Part 2: " + str(number_hits_part2))
  48.   refresh_display()
  49.  
  50. def update_instruction():
  51.   global number_instructions_so_far
  52.   canvas.itemconfigure(instruction_id, text=instruction)
  53.   number_instructions_so_far += 1
  54.   canvas.itemconfigure(number_instructions_id, text=str(number_instructions_so_far) + " of " + str(number_instructions))
  55.   refresh_display()
  56.  
  57. # *** graphics setup ***
  58.  
  59. root = Tk()
  60. canvas = Canvas(root, width=500, height=500, background='white')
  61.  
  62. # without this, the window would render as empty
  63. canvas.pack()
  64.  
  65. # draw outline of dial
  66. # coordinates = left/top/right/bottom of bounding box
  67. canvas.create_oval(50, 50, 450, 450, fill='white', outline='black', width=5)
  68.  
  69. # draw labels around dial
  70. for index in range(0, 100, 5):
  71.   radians = get_radians(index)
  72.   x = get_x(index, 225)
  73.   y = get_y(index, 225)
  74.   canvas.create_text(x, y, text=str(index))
  75.  
  76. # create pointer_ids
  77. for index in range(0, 100):
  78.   x = get_x(index, 175)
  79.   y = get_y(index, 175)
  80.   pointer_ids.append(canvas.create_line(250, 250, x, y, fill='black', arrow=LAST, state=HIDDEN))
  81.  
  82. # draw pointer
  83. pointer = 50
  84. draw_pointer()
  85.  
  86. # draw stuff for current instruction
  87. canvas.create_text(75, 25, text="Instruction:")
  88. instruction_id = canvas.create_text(25, 50, text="")
  89.  
  90. # draw stuff for number of instructions
  91. canvas.create_text(400, 25, text="Instruction count:")
  92. number_instructions_id = canvas.create_text(440, 50, text="")
  93.  
  94. canvas.create_text(450, 450, text="Password:")
  95. password_part1_id = canvas.create_text(450, 470, text="")
  96. password_part2_id = canvas.create_text(450, 485, text="")
  97. update_password()
  98.  
  99. # *** solve the puzzle ***
  100.  
  101. time.sleep(10) # allow time to start screen capture
  102.  
  103. file_preview = open("1_sample.txt", "r")
  104. number_instructions = 0
  105. for line in file_preview:
  106.   number_instructions += 1
  107.  
  108. file = open("1_sample.txt", "r")
  109. for line in file:
  110.   instruction = line.replace("\n", "")
  111.   update_instruction()
  112.   direction = -1 if instruction[0] == "L" else 1
  113.   for steps in range(0, int(instruction[1:])):
  114.     pointer = (pointer + direction) % 100
  115.     draw_pointer()
  116.     time.sleep(0.01)
  117.     if pointer == 0:
  118.       number_hits_part2 += 1
  119.       update_password()
  120.       time.sleep(0.1)
  121.   if pointer == 0:
  122.     number_hits_part1 += 1
  123.     update_password()
  124.     time.sleep(0.1)
  125.  
  126. canvas.create_text(50, 450, text="Done!")
  127. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment