-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.py
169 lines (132 loc) · 5.26 KB
/
wordle.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from collections import defaultdict
from dataclasses import dataclass
import json
from typing import Callable, Dict, Iterable, List, Self, Set, Tuple
from itertools import product
from math import log2
import string
import time
FilterFn = Callable[[str], bool]
@dataclass
class IncludeCharFilter:
char: str
def __call__(self, candidate: str):
return self.char in candidate
def __hash__(self) -> int:
return hash((self.char, "includes"))
def __eq__(self, __value: Self) -> bool:
return self.char == __value.char
@dataclass
class HasCharInPosFilter:
char: str
pos: int
def __call__(self, candidate: str):
return candidate[self.pos] == self.char
def __hash__(self) -> int:
return hash((self.char, self.pos, "haspos"))
def __eq__(self, __value: Self) -> bool:
return self.char == __value.char and self.pos == __value.pos
@dataclass
class DoesNotHaveCharFilter:
char: str
def __call__(self, candidate: str):
return self.char not in candidate
def __hash__(self) -> int:
return hash((self.char, "doesnot"))
def __eq__(self, __value: Self) -> bool:
return self.char == __value.char
@dataclass
class StepResult:
new_possibilities: Set[str]
expected_information: float
actual_information: float
def information(p: float):
if p <= 0:
return 0
return -log2(p)
def expected_information(p: float):
return p * information(p)
class Engine:
def __init__(self, possible_words: List[str]):
self.init_guesses(possible_words)
self.current_filters: List[Tuple[FilterFn]] = []
def init_guesses(self, guesses: Iterable[str]):
self.possible_words = set(guesses)
self.includeset_dict = self.build_includeset_dict()
def build_word_filtersets(self, word: str) -> Iterable[Tuple[FilterFn]]:
filters_per_char: List[List[FilterFn]] = []
seen_chars: Set[str] = set()
for i, char in enumerate(word):
position_filters = [DoesNotHaveCharFilter(char=char),
HasCharInPosFilter(char=char, pos=i)]
if char not in seen_chars:
position_filters.append(IncludeCharFilter(char=char))
seen_chars.add(char)
filters_per_char.append(position_filters)
inner_product: Iterable[Tuple[FilterFn, ...]] = product(*filters_per_char) # noqa
return inner_product
def build_word_includeset(self, word: str) -> Set[FilterFn]:
alphabet = set(string.ascii_lowercase)
filters_per_char: Set[FilterFn] = set()
word_alphabet = set(word)
seen_chars: Set[str] = set()
for i, char in enumerate(word):
filters_per_char.add(HasCharInPosFilter(char, i))
if not char in seen_chars:
filters_per_char.add(IncludeCharFilter(char))
seen_chars.add(char)
for missing_char in (alphabet - word_alphabet):
filters_per_char.add(DoesNotHaveCharFilter(missing_char))
return filters_per_char
def filter_current_guesses(self, filter_sets: Iterable[Tuple[FilterFn]]):
word_sets: List[Set[str]] = []
for filter_set in filter_sets:
partial_filtered_words = [self.includeset_dict[filter]
for filter in filter_set]
word_set = self.possible_words.intersection(*partial_filtered_words) # noqa
word_sets.append(word_set)
return word_sets
def entropy(self, word: str):
filter_sets = self.build_word_filtersets(word)
word_sets = self.filter_current_guesses(filter_sets)
entropy = sum(expected_information(len(word_set) / len(self.possible_words))
for word_set in word_sets)
return entropy
def build_includeset_dict(self):
all_filters: Dict[FilterFn, Set[str]] = defaultdict(set)
for word in self.possible_words:
include_sets = self.build_word_includeset(word)
for f in include_sets:
all_filters[f].add(word)
return all_filters
def narrow_guesses(self, filters: Tuple[FilterFn]):
new_wordset = self.filter_current_guesses([filters])[0]
return new_wordset
def step(self, word: str, new_filter_set: Tuple[FilterFn, ...]) -> StepResult:
new_guesses = self.narrow_guesses(new_filter_set)
p = len(new_guesses) / len(self.possible_words)
actual_info = information(p)
expected_info = self.entropy(word)
self.init_guesses(new_guesses)
self.current_filters.append(new_filter_set)
return StepResult(
new_possibilities=new_guesses,
expected_information=expected_info,
actual_information=actual_info)
def main():
words: List[str] = json.load(open("guesses.json"))
e = Engine(possible_words=words)
start = time.time()
result = e.step(
"snake", (
DoesNotHaveCharFilter("s"),
DoesNotHaveCharFilter("n"),
HasCharInPosFilter("a", pos=2),
HasCharInPosFilter("k", pos=3),
HasCharInPosFilter("e", pos=4)
))
end = time.time()
print((end - start))
print(result.actual_information, result.expected_information,
len(result.new_possibilities))
main()