-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.pro
347 lines (287 loc) · 7.98 KB
/
wordle.pro
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
% wordle.pro --- Play wordle with german or english words
%
% Author: Stefan Möding <stm@kill-9.net>
% Created: <2022-03-29 18:37:22 stm>
% Updated: <2023-03-05 12:10:36 stm>
%
% wordle(?atom, ?integer)
%
% wordle(Word, Weight) succeeds if the letters used in Word have a total
% weight of Weight.
%
:- dynamic(wordle/2).
% weight(?atom, ?integer)
%
% weight(Char, Weight) succeeds if Char has Weight as determined by the
% letter frequency for the processed words.
%
:- dynamic(weight/2).
% incweight(+list)
%
% incweight(List) iterates over a list of single char atoms. For each char it
% checks if weight(Char, Weight) can be proved. If the clause weight(Char,
% Weight) exists, then the fact is retracted and a new fact with Weight+1 is
% asserted. Otherwise weight(Char, 1) is asserted.
%
incweight([]).
incweight([H|T]) :-
clause(weight(H, Old), _),
New is Old + 1,
retract(weight(H, Old)),
asserta(weight(H, New)),
!,
incweight(T).
incweight([H|T]) :-
\+ clause(weight(H, _), _),
asserta(weight(H, 1)),
!,
incweight(T).
% learn_letters(+list)
%
% learn_letters(List) iterates over a list of words. Each word is decomposed
% into a list of chars and the weight for these chars is incremented.
%
learn_letters([]).
learn_letters([H|T]) :-
atom_chars(H, Chars),
incweight(Chars),
!,
learn_letters(T).
% getweight(?list, ?integer)
%
% getweight(List, Weight) succeeds if the total weights for all letters in
% List is Weight. If a letter is used more than once in a word, only one
% occurrence is used to calculate the weight. This is used to prefer words
% with different letters over words where some letters are used more than
% once.
%
getweight([], 0).
getweight([H|T], Weight) :-
weight(H, W1),
delete(T, H, Rest),
getweight(Rest, W2),
Weight is W1 + W2.
% read_line_to_codes(+stream, ?list)
%
% read_line_to_codes(Stream, Codes) succeeds if Codes is the list of
% character codes that make up a line read from Stream.
%
%
read_line_to_codes(Stream, Codes) :-
get_code(Stream, C0),
( C0 == -1
-> Codes0 = end_of_file
; read_1line_to_codes(C0, Stream, Codes0)
),
Codes = Codes0.
read_1line_to_codes(-1, _, []) :- !.
read_1line_to_codes(10, _, []) :- !.
read_1line_to_codes(13, Stream, L) :-
!,
get_code(Stream, C2),
read_1line_to_codes(C2, Stream, L).
read_1line_to_codes(C, Stream, [C|T]) :-
get_code(Stream, C2),
read_1line_to_codes(C2, Stream, T).
% read_words(+stream, ?list)
%
% read_words(Stream, List) succeeds if List is a list of words read from
% Stream.
%
read_words(Stream, []) :-
at_end_of_stream(Stream).
read_words(Stream, [H|T]) :-
\+ at_end_of_stream(Stream),
read_line_to_codes(Stream, Line),
atom_codes(H, Line),
read_words(Stream, T),
!.
% learn_words(+list)
%
% learn_words(List) iterates over List and adds wordle(Word, Weight) facts to
% the knowledge base as a side effect. Word is an atom of the word and Weight
% the total sum of the weight of the characters that are contained in Word.
%
learn_words([]).
learn_words([H|T]) :-
atom_chars(H, Chars),
getweight(Chars, Weight),
assertz(wordle(H, Weight)),
learn_words(T).
% play(+atom)
%
% play(Language) initializes the wordle(Word, Weight) fact for all words read
% from the file Language.
%
% play must be called to play a new game.
%
play(Language) :-
randomize,
% Forget all words and weights
retractall(wordle(_, _)),
retractall(weight(_, _)),
% Read words from file
open(Language, read, Str),
read_words(Str, Words),
close(Str),
% Store weights for used letters in knowledge base
learn_letters(Words),
% Store words in knowledge base
learn_words(Words).
german :- play(german).
english :- play(english).
% write_candidates(-atom)
%
% write_candidates(Count) writes that Count candidates are remaining as side
% effect.
%
write_candidates(1) :- write('1 candidate remains.'), nl, !.
write_candidates(Count) :- write(Count), write(' candidates remain.'), nl, !.
% write_words(+list)
%
% write_words(List) writes all elements of List as a side effect.
%
write_words([]).
write_words([H|T]) :- write(H), nl, write_words(T).
% words
% words(-list)
%
% words writes all Words for which wordle(Word, _) can be proved. It also
% writes the number of items found.
%
% words(Words) instantiates the variable Words with the list of words for
% which wordle(Word, _) can be proved.
%
words :-
findall(X, wordle(X, _), Words),
write_words(Words),
length(Words, Count),
write_candidates(Count).
words(Words) :-
findall(X, wordle(X, _), Words).
% count(+atom, +list, ?integer)
%
% count(Element, List, Num) succeeds if Element occurs exactly Num times in
% List.
%
count(Element, List, 0) :-
\+ memberchk(Element, List),
!.
count(Element, List, Number) :-
memberchk(Element, List),
select(Element, List, List2),
count(Element, List2, Remaining),
Number is Remaining + 1,
!.
% gray(+atom)
%
% gray(Char) retracts all wordle(Word, _) facts that do not have Char as
% a character in Word.
%
gray(Char) :- gray(Char, 1).
% gray(+atom, +integer)
%
% gray(Char, Count) retracts all wordle(Word, _) facts that do have Count or
% more occurences of Char. This is used if you have the same character marked
% as green/yellow and as gray. In this case you use gray(Char, 2) to indicate
% that the character is not used two or more times.
%
gray(Char, Count) :-
findall(X, wordle(X, _), Words),
gray(Char, Count, Words),
!.
gray(_, _, []).
gray(Char, Count, [H|T]) :-
gray1(Char, Count, H),
gray(Char, Count, T).
gray1(Char, Count, Word) :-
atom_chars(Word, List),
count(Char, List, Num),
Num >= Count,
retract(wordle(Word, _)).
gray1(Char, Count, Word) :-
atom_chars(Word, List),
count(Char, List, Num),
Num < Count.
% green(+atom, +integer)
%
% green(Char, Pos) retracts all wordle(Word, _) facts that do not have Char
% as a character at position Pos in Word.
%
green(Char, Pos) :-
findall(X, wordle(X, _), Words),
green(Char, Pos, Words),
!.
green(_, _, []).
green(Char, Pos, [H|T]) :-
green1(Char, Pos, H),
green(Char, Pos, T).
green1(Char, Pos, Word) :-
atom_chars(Word, List),
nth(Pos, List, Wordchar),
Char \= Wordchar,
retract(wordle(Word, _)).
green1(Char, Pos, Word) :-
atom_chars(Word, List),
nth(Pos, List, Wordchar),
Char = Wordchar.
% yellow(+atom, +integer)
%
% yellow(Char, Pos) retracts all wordle(Word, _) that either have Char at
% position Pos in Word or do not have Char at one of the other positions.
%
yellow(Char, Pos) :-
findall(X, wordle(X, _), Words),
yellow(Char, Pos, Words),
!.
yellow(_, _, []).
yellow(Char, Pos, [H|T]) :-
yellow1(Char, Pos, H),
yellow(Char, Pos, T).
yellow1(Char, Pos, Word) :-
atom_chars(Word, List),
nth(Pos, List, Wordchar),
Char = Wordchar,
retract(wordle(Word, _)).
yellow1(Char, Pos, Word) :-
atom_chars(Word, List),
nth(Pos, List, Wordchar),
Char \= Wordchar,
\+ memberchk(Char, List),
retract(wordle(Word, _)).
yellow1(Char, Pos, Word) :-
atom_chars(Word, List),
nth(Pos, List, Wordchar),
Char \= Wordchar,
memberchk(Char, List).
% bestguess(-list)
%
% bestguess(Words) instantiates Words with a list of Word for which
% wordle(Word, Weight) can be proved and Weight is the maximum weight.
%
bestguess(Words) :-
findall(W, wordle(_, W), Weights),
max_list(Weights, Max),
findall(X, wordle(X, Max), Words).
% bestguess1(-atom)
%
% bestguess1(Word) instantiates Word with a random word in list Words for
% which bestguess(Words) can be proved.
%
bestguess1(Word) :-
bestguess(Words),
length(Words, Count),
random(0, Count, Pos),
Pos1 is Pos + 1,
nth(Pos1, Words, Word).
% guess(-atom)
%
% guess(Word) instantiates Word with a random Word for which wordle(Word, _)
% can be proved.
%
guess(Word) :-
findall(X, wordle(X, _), Words),
length(Words, Count),
random(0, Count, Pos),
Pos1 is Pos + 1,
nth(Pos1, Words, Word).