-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_dict.rs
185 lines (158 loc) · 5.17 KB
/
backup_dict.rs
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
use core::panic;
use crate::{xordle::Word, Feedback, Letter, WORD_LEN};
pub const _DECISION_CAP: usize = 50;
/// We have 12 947 words with frequency data from the google 5-gram data set
const DICTIONARY_SIZE: usize = 12_947;
pub struct Dictionary {
pub words: Vec<[u8; WORD_LEN]>,
pub counts: Vec<u64>,
}
impl Dictionary {
pub fn new() -> Self {
let words_array = include!("words.in");
let mut words = Vec::with_capacity(DICTIONARY_SIZE);
words.extend_from_slice(&words_array);
let counts_array: [u64; DICTIONARY_SIZE] = include!("counts.in");
let mut counts = Vec::with_capacity(DICTIONARY_SIZE);
counts.extend_from_slice(&counts_array);
Self { words, counts }
}
#[inline(always)]
pub fn match_count(&self, guess: Word, feedback: Feedback) -> u64 {
let mut used: [bool; 5] = [false; WORD_LEN];
self.words
.iter()
// .take(DECISION_CAP)
.filter(|&word| matches(&mut used, *word, guess, feedback))
.count()
.try_into()
.expect("match count fits in a u64")
}
pub fn match_count_occurence_sum(&self, guess: Word, feedback: Feedback) -> u64 {
let mut used: [bool; 5] = [false; WORD_LEN];
self.words
.iter()
.zip(&self.counts)
.filter_map(|(word, count)| matches(&mut used, *word, guess, feedback).then_some(count))
.sum()
}
pub fn trim(&mut self, guess: Word, feedback: Feedback) {
let mut rejected_indeces = Vec::new();
let mut used: [bool; 5] = [false; WORD_LEN];
let solved = set_solved(guess, feedback);
let present = set_present(guess, feedback);
for (index, &word) in self.words.iter().enumerate() {
let v1 = matches_v1(&mut used, word, guess, feedback);
let v2 = matches_v2(&solved, present.clone(), word, guess, feedback);
// if v1 != v2 && guess != *b"state" {
// println!(
// "{} - old {} new {}",
// std::str::from_utf8(&word).unwrap(),
// v1,
// v2
// );
// assert_eq!(v1, v2);
// }
if !v2 {
rejected_indeces.push(index);
}
}
for index in rejected_indeces.into_iter().rev() {
self.words.remove(index);
self.counts.remove(index);
}
}
}
fn set_solved(guess: [u8; 5], feedback: Feedback) -> [u8; 26] {
let mut solved = [WORD_LEN as u8; (b'z' - b'a' + 1) as usize];
for (i, (mark, letter)) in feedback.iter().zip(guess).enumerate() {
if let Letter::Solved = mark {
solved[(letter - b'a') as usize] = i as u8;
}
}
solved
}
fn set_present(guess: [u8; 5], feedback: Feedback) -> [u8; 26] {
let mut present = [0u8; (b'z' - b'a' + 1) as usize];
for (mark, letter) in feedback.iter().zip(guess) {
if let Letter::Solved | Letter::Misput = mark {
present[(letter - b'a') as usize] += 1;
}
}
present
}
impl Default for Dictionary {
fn default() -> Self {
Self::new()
}
}
// fn matches_v3
fn matches_v2(
solved: &[u8],
mut present: [u8; 26],
word: Word,
guess: Word,
feedback: Feedback,
) -> bool {
for ((mark, letter), guess) in feedback.iter().zip(word).zip(guess) {
let solved_index = solved[(letter - b'a') as usize] as usize;
if solved_index < WORD_LEN {
if letter != word[solved_index] {
return false;
}
}
if letter == guess {
if let Letter::Misput = mark {
return false;
}
}
}
for letter in word {
for (&mark, marked) in feedback.iter().zip(guess) {
if mark != Letter::Absent {
continue;
}
if letter == marked {
if present[(letter - b'a') as usize] == 0 {
return false;
} else {
present[(letter - b'a') as usize] -= 1;
break;
}
}
}
}
true
}
fn matches_v1(used: &mut [bool], word: Word, guess: Word, feedback: Feedback) -> bool {
used.fill(false);
for (((used, mark), letter), guess) in used.iter_mut().zip(feedback).zip(word).zip(guess) {
if letter == guess {
match mark {
Letter::Misput => return false,
Letter::Solved => *used = true,
_ => (),
}
}
}
for (&mark, guess) in feedback.iter().zip(guess) {
for (used, letter) in used.iter_mut().zip(word) {
if *used {
continue;
}
if letter == guess {
match mark {
Letter::Absent => return false,
Letter::Misput => {
*used = true;
}
_ => (),
}
}
}
}
true
}
fn matches(used: &mut [bool], word: Word, guess: Word, feedback: Feedback) -> bool {
matches_v1(used, word, guess, feedback)
}