Skip to content

Instantly share code, notes, and snippets.

@colrdavidson
Last active June 23, 2022 21:31
Show Gist options
  • Save colrdavidson/c2b15f7ec3fa2b80e3fa51b83802a0cd to your computer and use it in GitHub Desktop.
Save colrdavidson/c2b15f7ec3fa2b80e3fa51b83802a0cd to your computer and use it in GitHub Desktop.
Wordle Solver
import sys
import random
if len(sys.argv) != 2:
print("Expected a date!")
sys.exit(1)
date = sys.argv[1]
tries = random.randint(2, 4)
print("Wordle %s %s/6" % (date, tries))
win = ("🟩" * 5)
won = False
width = 5
board = []
for i in range(tries - 1):
attempt = ""
for j in range(width):
rval = random.randint(0, 2)
if rval == 0:
attempt += "⬛"
elif rval == 1:
attempt += "🟨"
else:
attempt += "🟩"
board.append(attempt)
if attempt == win:
won = True
break
for attempt in board:
print(attempt)
if not won:
for i in range(width):
print("🟩", end="")
print("")
quips = [
"Proud of that one, got lucky though!",
"robbed",
"My first 2/6!!",
"That was lucky.",
"Third guess was not even legal",
"Absolute sausage",
"😢",
"Lol",
"It did take 22 mins",
"not the best result, but given what i had to go on, that was a pretty good guess",
"It's almost as if we guess differently.",
"wow. tried a new starter",
"lava would be positively refreshing",
"could have been two @cloin's bot",
"Missed opportunity for trash talking bins.",
"What a weird one",
"All you people getting 2 with your clever openers",
"Hmm. BONOR indeed",
"groan",
"I'm just getting used to wordle, and my pattern matching skills suck ass",
"Not my best day.",
"hmpf",
"this one doesn't count, right?",
"more hard mode",
"owie",
"Looks like a snail.",
"trying out hard mode"
]
raw_quip = True
if raw_quip:
quipdx = random.randint(0, len(quips) - 1)
print(quips[quipdx])
else:
class Node:
def __init__(self, val=""):
self.val = val
self.next = []
full_tokens = []
for quip in quips:
tokens = quip.split()
for token in tokens:
full_tokens.append(token)
def get_node(chain, key):
for n in chain:
if n.val == key:
return n
return Node(key)
def is_in_chain(chain, node):
for i, cur in enumerate(chain):
if node.val == cur.val:
return True, i
return False, -1
chain = []
for i, token in enumerate(full_tokens):
node = get_node(chain, token)
if i+1 < len(full_tokens):
node.next.append(full_tokens[i+1])
is_in, i = is_in_chain(chain, node)
if not is_in:
chain.append(node)
else:
chain[i] = node
MAX_LEN = random.randint(0, 20)
start_idx = random.randint(0, len(chain) - 1)
output = chain[start_idx].val
next_out = output
for i in range(MAX_LEN):
node = get_node(chain, next_out)
next_tok = ""
if len(node.next) != 0:
next_idx = random.randint(0, len(node.next) - 1)
next_tok = node.next[next_idx]
output += " " + next_tok
next_out = next_tok
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment