Optimisations

This commit is contained in:
Luke Hubmayer-Werner 2022-02-04 21:42:42 +10:30
parent b78db00f90
commit eeca1d3a49
1 changed files with 16 additions and 26 deletions

View File

@ -78,7 +78,6 @@ fn char2bit(c: Achar) -> Charmask {
fn cm2char(cm: Charmask, offset: i8) -> Achar { fn cm2char(cm: Charmask, offset: i8) -> Achar {
(((31 - cm.lzcnt() as i8) + A + offset) as u8) as Achar (((31 - cm.lzcnt() as i8) + A + offset) as u8) as Achar
//(((cm.tzcnt() as i8) + A + offset) as u8) as Achar
} }
fn _generate_wordcache_nested(cache: &mut WordCache, subcache: &[Word], key: Charmask, depth: u8) { fn _generate_wordcache_nested(cache: &mut WordCache, subcache: &[Word], key: Charmask, depth: u8) {
@ -103,39 +102,30 @@ fn generate_wordcache(words: Vec<Word>) -> WordCache {
cache cache
} }
fn filter_word(w: &Word, banned_chars: &[Charmask; WORD_LENGTH_P]) -> bool { fn filter_word(w: &[Charmask; WORD_LENGTH_P], banned_chars: &[Charmask; WORD_LENGTH_P]) -> bool {
for (cb, bans) in w.charbits.iter().zip(banned_chars.iter()) { for i in 0..WORD_LENGTH {
if cb & bans != 0 { if w[i] & banned_chars[i] != 0 {return false;}
return false;
}
} }
true true
} }
fn simulate(guess: &Word, solution: &Word, mut s: SimState, wordcache: &WordCache) -> (usize, SimState) { fn simulate(guess: &Word, solution: &Word, mut s: SimState, wordcache: &WordCache) -> usize {
s.required_chars |= guess.charmask & solution.charmask; s.required_chars |= guess.charmask & solution.charmask;
for (i, (gc, sc)) in guess.letters.iter().zip(solution.letters.iter()).enumerate() { let bans = guess.charmask & !solution.charmask;
let gb = char2bit(*gc); for j in 0..s.banned_chars.len() {
if gc == sc { // Right letter right position s.banned_chars[j] |= bans;
s.banned_chars[i] = !gb; }
} else if solution.charmask & gb != 0 { // Right letter wrong position for i in 0..WORD_LENGTH {
s.banned_chars[i] |= gb; if guess.letters[i] == solution.letters[i] { // Right letter right position
} else { // Letter not in solution s.banned_chars[i] = !guess.charbits[i];
for j in 0..s.banned_chars.len() { } else if guess.charbits[i] & solution.charmask != 0 { // Right letter wrong position
s.banned_chars[j] |= gb; s.banned_chars[i] |= guess.charbits[i];
}
} }
} }
let cachekey = s.required_chars; let cachekey = s.required_chars;
match wordcache.contains_key(&cachekey) { match wordcache.contains_key(&cachekey) {
true => ( true => wordcache[&cachekey].iter().filter(|w| filter_word(&w.charbits, &s.banned_chars)).count(),
wordcache[&cachekey].iter().filter(|w| filter_word(w, &s.banned_chars)).count(), false => 0,
s
),
false => (
0,
s
),
} }
} }
@ -144,7 +134,7 @@ fn find_worstcase(word: &Word, wordcache: &WordCache) -> (String, usize) {
let mut worst_w = wordcache[&0][0].letters; let mut worst_w = wordcache[&0][0].letters;
let ss = SimState::default(); let ss = SimState::default();
for target in &wordcache[&0] { for target in &wordcache[&0] {
let remaining = simulate(word, target, ss, wordcache).0; let remaining = simulate(word, target, ss, wordcache);
if remaining > worst { if remaining > worst {
worst = remaining; worst = remaining;
worst_w = target.letters; worst_w = target.letters;