ok i'm going to remove the hashset
This commit is contained in:
parent
d3f89edddf
commit
7cbaf40d44
1 changed files with 20 additions and 22 deletions
40
src/main.rs
40
src/main.rs
|
|
@ -35,7 +35,7 @@ fn solve(dictionary: &mut [(String, LetterSet)], target: LetterSet) -> (u8, Hash
|
|||
println!("allocated!");
|
||||
bests[LetterSet::FULL.to_index()] = (0, HashSet::new());
|
||||
|
||||
score(dictionary, &mut bests, LetterSet::EMPTY, 0);
|
||||
score(dictionary, &mut bests, target.negation(), 0);
|
||||
|
||||
bests[target.negation().to_index()].clone()
|
||||
}
|
||||
|
|
@ -51,33 +51,31 @@ fn score(
|
|||
done_letters: LetterSet,
|
||||
length: u8,
|
||||
) -> u8 {
|
||||
assert!(length <= MAX_SOLUTION_LENGTH);
|
||||
if length > MAX_SOLUTION_LENGTH {
|
||||
return u8::MAX;
|
||||
}
|
||||
|
||||
let known = bests[done_letters.to_index()].0;
|
||||
if known < u8::MAX {
|
||||
return known;
|
||||
}
|
||||
|
||||
for (i, (word, ls)) in dictionary
|
||||
let Some((word, ls, score)) = dictionary
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, (_, ls))| !ls.difference(done_letters).is_empty())
|
||||
{
|
||||
let keep_letters = done_letters.union(*ls);
|
||||
let keep_length = length.saturating_add(u8::try_from(word.len()).unwrap());
|
||||
.filter(|(_, ls)| !ls.difference(done_letters).is_empty())
|
||||
.map(|(word, ls)| {
|
||||
let word_len = u8::try_from(word.len()).unwrap();
|
||||
let new_length = word_len.saturating_add(length);
|
||||
let rest_score = score(dictionary, bests, done_letters.union(*ls), new_length);
|
||||
(word, ls, word_len.saturating_add(rest_score))
|
||||
})
|
||||
.min_by_key(|(_, _, score)| *score)
|
||||
else {
|
||||
return u8::MAX;
|
||||
};
|
||||
|
||||
if keep_length > MAX_SOLUTION_LENGTH {
|
||||
continue;
|
||||
}
|
||||
|
||||
let keep = score(dictionary, bests, keep_letters, keep_length);
|
||||
|
||||
let existing = bests[done_letters.to_index()].0;
|
||||
if keep < existing {
|
||||
bests[done_letters.to_index()].0 = keep;
|
||||
bests[done_letters.to_index()].0 = score;
|
||||
bests[done_letters.to_index()].1 = bests[done_letters.union(*ls).to_index()].1.clone();
|
||||
bests[done_letters.to_index()].1.insert(word.clone());
|
||||
}
|
||||
}
|
||||
|
||||
bests[done_letters.to_index()].0
|
||||
score
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue