-
Notifications
You must be signed in to change notification settings - Fork 0
/
absurdle.py
76 lines (64 loc) · 3.18 KB
/
absurdle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from collections import defaultdict
from random import choice as randomChoice
GREENSQUARE, YELLOWSQUARE, GRAYSQUARE = "🟩", "🟨", "⬛"
memo = {}
def generateFeedback(guessWord, correctWord):
if (guessWord, correctWord) in memo:
return memo[(guessWord, correctWord)]
feedback = [GRAYSQUARE for i in range(5)]
yellowFeedbackUnusedLetters = list(correctWord)
for i, (guessWordLetter, correctWordLetter) in enumerate(zip(list(guessWord), list(correctWord))):
if guessWordLetter == correctWordLetter:
feedback[i] = GREENSQUARE
yellowFeedbackUnusedLetters.remove(correctWordLetter)
for i, (guessWordLetter, correctWordLetter) in enumerate(zip(list(guessWord), list(correctWord))):
if guessWordLetter in yellowFeedbackUnusedLetters and feedback[i] != GREENSQUARE:
feedback[i] = YELLOWSQUARE
yellowFeedbackUnusedLetters.remove(guessWordLetter)
stringFeedback = "".join(feedback)
memo[(guessWord, correctWord)] = stringFeedback
return stringFeedback
def allLargestBuckets(buckets):
largestBucketSize = 0
largestBucketVals = []
for bucketFeedback in buckets:
if len(buckets[bucketFeedback]) > largestBucketSize:
largestBucketSize = len(buckets[bucketFeedback])
largestBucketVals = [bucketFeedback]
elif len(buckets[bucketFeedback]) == largestBucketSize:
largestBucketVals.append(bucketFeedback)
# more adversarial
if len(largestBucketVals) >= 2:
if GREENSQUARE * 5 in largestBucketVals:
largestBucketVals.remove(GREENSQUARE * 5)
# feedback
if len(largestBucketVals) >= 2 and __name__ == "__main__":
print(f"Multiple largest buckets, choosing between {len(largestBucketVals)}")
print("\n".join([f"{val}: {', '.join(buckets[val])}" for val in largestBucketVals]))
return largestBucketVals
def playAbsurdle(guess: str, wordbank: set) -> str:
if len(guess) != 5:
if __name__ == "__main__": print("Bad input. Try again.")
return ("", wordbank, True)
buckets = defaultdict(set)
for word in wordbank:
buckets[generateFeedback(guess, word)].add(word)
# largest bucket calculations
largestBucketVal = randomChoice(allLargestBuckets(buckets))
largestBucket = buckets[largestBucketVal]
if __name__ == "__main__":
[print(f"{feedback}: {word}") for feedback, word in sorted([(feedback, len(words)) for feedback, words in buckets.items()], key=lambda x: x[1], reverse=True)]
print(largestBucketVal, " ".join(sorted(list(largestBucket))))
return (largestBucketVal, largestBucket, False)
if __name__ == "__main__":
# local wordbank
mystery_words_file = open("absurdle_mystery_words.txt", "r")
wordbank = set(mystery_words_file.read().splitlines())
while True:
feedback, wordbank, error = playAbsurdle(input("Guess a word: "), wordbank)
if feedback == GREENSQUARE * 5:
print("You win!")
more = input("Continue? (y/n) ")
if more == "n": break
mystery_words_file = open("absurdle_mystery_words.txt", "r")
wordbank = set(mystery_words_file.read().splitlines())