Created
March 27, 2025 11:47
-
-
Save webdevwilson/8875b3276be9b7b9993c9b54dd9a5f92 to your computer and use it in GitHub Desktop.
A birthday message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
import sys | |
COLORS = { | |
'red': '\033[0;31m', | |
'yellow': '\033[0;33m', | |
'green': '\033[0;32m', | |
'blue': '\033[0;34m', | |
'purple': '\033[0;35m', | |
'white': '\033[0;37m', | |
} | |
RESET = '\033[0m' | |
random_colors = [COLORS['red'], COLORS['yellow'], COLORS['green'], COLORS['blue'], COLORS['purple']] | |
target = "Happy Birthday, Dad!" | |
# ASCII 32 to 126 are the printable characters | |
current_state = [chr(random.SystemRandom().randint(32, 126)) for _ in target] | |
rand = random.SystemRandom() | |
while ''.join(current_state) != target: | |
# Update each character that doesn't match the target | |
for i in range(len(target)): | |
if current_state[i] != target[i]: | |
current_state[i] = chr(rand.randint(32, 126)) | |
# Clear the terminal screen and move cursor to top-left | |
print('\033[2J\033[H', end='') | |
for char, target_char in zip(current_state, target): | |
if char == target_char: | |
# Matched characters are white | |
color = COLORS['white'] | |
else: | |
# Unmatched characters get a random color | |
color = rand.choice(random_colors) | |
print(color + char, end='') | |
print(RESET, end='') | |
sys.stdout.flush() | |
time.sleep(0.02) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment