Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Input: lines like "L<number>" or "R<number>"
- # Output: number starts at 50, L decreases, R increases
- # range is 0 to 99 (out of bounds -> wrap around)
- # how many times does it hit 0?
- # part 1: at the end of a move / part 2: also mid-move
- from tkinter import *
- import math
- old_pointer = -1
- number_hits_a = 0
- number_hits_b = 0
- number_instructions_so_far = 0
- # *** graphics functions ***
- def get_radians(index):
- return math.tau * index / 100
- def get_x(index, distance):
- return int(250 + math.sin(get_radians(index)) * distance)
- def get_y(index, distance):
- return int(250 - math.cos(get_radians(index)) * distance)
- def draw_or_erase_pointer(p, is_draw):
- x = get_x(p, 175)
- y = get_y(p, 175)
- canvas.create_line(250, 250, x, y, fill='black' if is_draw else 'white', arrow=LAST)
- def refresh_display():
- root.update_idletasks()
- root.update()
- def draw_pointer():
- global old_pointer # required to be able to alter it
- if old_pointer >= 0:
- draw_or_erase_pointer(old_pointer, is_draw=False)
- old_pointer = pointer
- draw_or_erase_pointer(pointer, is_draw=True)
- refresh_display()
- def update_password():
- canvas.create_rectangle(400, 460, 495, 495, fill='white', outline='white')
- canvas.create_text(450, 470, text="Part 1: " + str(number_hits_a))
- canvas.create_text(450, 485, text="Part 2: " + str(number_hits_b))
- refresh_display()
- def update_instruction():
- global number_instructions_so_far
- canvas.create_rectangle(10, 40, 45, 70, fill='white', outline='white')
- canvas.create_text(25, 50, text=instruction)
- number_instructions_so_far += 1
- canvas.create_rectangle(395, 40, 495, 65, fill='white', outline='white')
- canvas.create_text(440, 50, text=str(number_instructions_so_far) + " of " + str(number_instructions))
- refresh_display()
- # *** graphics setup ***
- root = Tk()
- canvas = Canvas(root, width=500, height=500, background='white')
- # without this, the window would render as empty
- canvas.pack()
- # draw outline of dial
- # coordinates = left/top/right/bottom of bounding box
- canvas.create_oval(50, 50, 450, 450, fill='white', outline='black', width=5)
- # draw labels around dial
- for index in range(0, 100, 5):
- radians = get_radians(index)
- x = get_x(index, 225)
- y = get_y(index, 225)
- canvas.create_text(x, y, text=str(index))
- # draw pointer
- pointer = 50
- draw_pointer()
- # draw fixed labels
- canvas.create_text(75, 25, text="Instruction:")
- canvas.create_text(400, 25, text="Instruction count:")
- canvas.create_text(450, 450, text="Password:")
- # *** solve the puzzle ***
- update_password()
- file_preview = open("1_input.txt", "r")
- number_instructions = 0
- for line in file_preview:
- number_instructions += 1
- file = open("1_input.txt", "r")
- for line in file:
- instruction = line.replace("\n", "")
- update_instruction()
- direction = -1 if instruction[0] == "L" else 1
- for steps in range(0, int(instruction[1:])):
- pointer = (pointer + direction) % 100
- draw_pointer()
- if pointer == 0:
- number_hits_b += 1
- update_password()
- if pointer == 0:
- number_hits_a += 1
- update_password()
- canvas.create_text(50, 450, text="Done!")
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment