Skip to content

Instantly share code, notes, and snippets.

@boppreh
Created October 30, 2024 15:19
Show Gist options
  • Save boppreh/ed35af46ab35f2bf0fb8e4220ca703fc to your computer and use it in GitHub Desktop.
Save boppreh/ed35af46ab35f2bf0fb8e4220ca703fc to your computer and use it in GitHub Desktop.
from collections import deque
def find_moves(target):
stack = deque([(0, 0, "")])
while True:
# current: how many X's we currently have.
# clipboard: how many X's were copied.
# history: sequence of "T", "C" and "P"
current, clipboard, history = stack.popleft()
if current >= target:
return history
stack.append((current+1, clipboard, history+"T"))
if current != clipboard:
stack.append((current, current, history+"C"))
stack.append((current+clipboard, clipboard, history+"P"))
moves = find_moves(200)
print(f'Found solution that takes {len(moves)} seconds: {moves}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment