Skip to content

Instantly share code, notes, and snippets.

@i-infra
Created December 19, 2024 04:27
Show Gist options
  • Save i-infra/ccc38648bc39fc819cef6981453b2376 to your computer and use it in GitHub Desktop.
Save i-infra/ccc38648bc39fc819cef6981453b2376 to your computer and use it in GitHub Desktop.
make_puzzle.py from claude
def caesar_shift(text, shift=3):
"""Perform a caesar shift on the input text."""
result = ""
for char in text:
if char.isalpha():
# Determine the case and base ASCII value
ascii_base = ord('A') if char.isupper() else ord('a')
# Shift the character and wrap around if necessary
shifted = (ord(char) - ascii_base + shift) % 26
result += chr(ascii_base + shifted)
else:
# Keep non-alphabetic characters unchanged
result += char
return result
def string_to_binary(text):
"""Convert a string to its binary representation."""
binary = ' '.join(format(ord(char), '08b') for char in text)
return binary
def create_binary_puzzle(input_string):
"""Convert input string to binary puzzle by:
1. Caesar shifting by 3
2. Reversing the string
3. Converting to binary
"""
# Step 1: Caesar shift
shifted = caesar_shift(input_string)
# Step 2: Reverse the string
reversed_string = shifted[::-1]
# Step 3: Convert to binary
binary_result = string_to_binary(reversed_string)
return binary_result
def solve_puzzle(binary_puzzle):
"""Solve the binary puzzle by reversing the operations."""
# Convert binary to text
binary_numbers = binary_puzzle.split()
text = ''.join(chr(int(bin_num, 2)) for bin_num in binary_numbers)
# Reverse the string
unreversed = text[::-1]
# Caesar shift backwards
solution = caesar_shift(unreversed, shift=-3)
return solution
def main():
# Get input from user
user_input = input("Enter a string to convert to a binary puzzle: ")
# Create the puzzle
result = create_binary_puzzle(user_input)
# Print results
print("\nOriginal string:", user_input)
print("Caesar shifted:", caesar_shift(user_input))
print("Reversed:", caesar_shift(user_input)[::-1])
print("Binary puzzle:", result)
print("Solved puzzle:", solve_puzzle(result))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment