Guest User

Untitled

a guest
Dec 13th, 2025
1,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 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.  
  10. old_pointer = -1
  11. number_hits_a = 0
  12. number_hits_b = 0
  13. number_instructions_so_far = 0
  14.  
  15. # *** graphics functions ***
  16.  
  17. def get_radians(index):
  18.   return math.tau * index / 100
  19.  
  20. def get_x(index, distance):
  21.   return int(250 + math.sin(get_radians(index)) * distance)
  22.  
  23. def get_y(index, distance):
  24.   return int(250 - math.cos(get_radians(index)) * distance)
  25.  
  26. def draw_or_erase_pointer(p, is_draw):
  27.   x = get_x(p, 175)
  28.   y = get_y(p, 175)
  29.   canvas.create_line(250, 250, x, y, fill='black' if is_draw else 'white', arrow=LAST)
  30.  
  31. def refresh_display():
  32.   root.update_idletasks()
  33.   root.update()
  34.  
  35. def draw_pointer():
  36.   global old_pointer # required to be able to alter it
  37.   if old_pointer >= 0:
  38.     draw_or_erase_pointer(old_pointer, is_draw=False)
  39.   old_pointer = pointer
  40.   draw_or_erase_pointer(pointer, is_draw=True)
  41.   refresh_display()
  42.  
  43. def update_password():
  44.   canvas.create_rectangle(400, 460, 495, 495, fill='white', outline='white')
  45.   canvas.create_text(450, 470, text="Part 1: " + str(number_hits_a))
  46.   canvas.create_text(450, 485, text="Part 2: " + str(number_hits_b))
  47.   refresh_display()
  48.  
  49. def update_instruction():
  50.   global number_instructions_so_far
  51.   canvas.create_rectangle(10, 40, 45, 70, fill='white', outline='white')
  52.   canvas.create_text(25, 50, text=instruction)
  53.   number_instructions_so_far += 1
  54.   canvas.create_rectangle(395, 40, 495, 65, fill='white', outline='white')
  55.   canvas.create_text(440, 50, text=str(number_instructions_so_far) + " of " + str(number_instructions))
  56.   refresh_display()
  57.  
  58. # *** graphics setup ***
  59.  
  60. root = Tk()
  61. canvas = Canvas(root, width=500, height=500, background='white')
  62.  
  63. # without this, the window would render as empty
  64. canvas.pack()
  65.  
  66. # draw outline of dial
  67. # coordinates = left/top/right/bottom of bounding box
  68. canvas.create_oval(50, 50, 450, 450, fill='white', outline='black', width=5)
  69.  
  70. # draw labels around dial
  71. for index in range(0, 100, 5):
  72.   radians = get_radians(index)
  73.   x = get_x(index, 225)
  74.   y = get_y(index, 225)
  75.   canvas.create_text(x, y, text=str(index))
  76.  
  77. # draw pointer
  78. pointer = 50
  79. draw_pointer()
  80.  
  81. # draw fixed labels
  82. canvas.create_text(75, 25, text="Instruction:")
  83. canvas.create_text(400, 25, text="Instruction count:")
  84. canvas.create_text(450, 450, text="Password:")
  85.  
  86. # *** solve the puzzle ***
  87.  
  88. update_password()
  89.  
  90. file_preview = open("1_input.txt", "r")
  91. number_instructions = 0
  92. for line in file_preview:
  93.   number_instructions += 1
  94.  
  95. file = open("1_input.txt", "r")
  96. for line in file:
  97.   instruction = line.replace("\n", "")
  98.   update_instruction()
  99.   direction = -1 if instruction[0] == "L" else 1
  100.   for steps in range(0, int(instruction[1:])):
  101.     pointer = (pointer + direction) % 100
  102.     draw_pointer()
  103.     if pointer == 0:
  104.       number_hits_b += 1
  105.       update_password()
  106.   if pointer == 0:
  107.     number_hits_a += 1
  108.     update_password()
  109.  
  110. canvas.create_text(50, 450, text="Done!")
  111. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment