keeps track of words, but obscenely slow
This commit is contained in:
parent
9eac56f50c
commit
8cba1ef0ce
1 changed files with 21 additions and 16 deletions
37
src/main.rs
37
src/main.rs
|
|
@ -1,5 +1,5 @@
|
|||
use std::{
|
||||
collections::{HashMap, hash_map::Entry},
|
||||
collections::HashMap,
|
||||
io::{self, BufRead, BufReader},
|
||||
};
|
||||
|
||||
|
|
@ -13,38 +13,43 @@ fn main() {
|
|||
.collect::<Result<_, _>>()
|
||||
.unwrap();
|
||||
|
||||
let mut min_lengths: HashMap<LetterSet, u16> = HashMap::new();
|
||||
let min_length = solve(dictionary.as_slice(), &mut min_lengths, LetterSet::FULL);
|
||||
println!("{min_length}");
|
||||
let mut bests = HashMap::new();
|
||||
let best = solve(dictionary.as_slice(), &mut bests, LetterSet::FULL);
|
||||
println!("{best:?}");
|
||||
}
|
||||
|
||||
// length doesn't include spaces. i think that's okay
|
||||
fn solve(
|
||||
dictionary: &[String],
|
||||
min_lengths: &mut HashMap<LetterSet, u16>,
|
||||
bests: &mut HashMap<LetterSet, (u16, Vec<String>)>,
|
||||
remaining: LetterSet,
|
||||
) -> u16 {
|
||||
) -> (u16, Vec<String>) {
|
||||
if remaining.is_empty() {
|
||||
return 0;
|
||||
return (0, Vec::new());
|
||||
}
|
||||
|
||||
if let Some(min) = min_lengths.get(&remaining) {
|
||||
return *min;
|
||||
if let Some(min) = bests.get(&remaining) {
|
||||
return min.clone();
|
||||
}
|
||||
|
||||
let min_length = dictionary
|
||||
let best = dictionary
|
||||
.iter()
|
||||
.filter(|word| remaining != remaining.difference(LetterSet::from(word.as_bytes())))
|
||||
.map(|word| {
|
||||
(u16::try_from(word.len()).unwrap()).saturating_add(solve(
|
||||
let (score, mut path) = solve(
|
||||
dictionary,
|
||||
min_lengths,
|
||||
bests,
|
||||
remaining.difference(LetterSet::from(word)),
|
||||
))
|
||||
);
|
||||
path.push(word.clone());
|
||||
(
|
||||
(u16::try_from(word.len()).unwrap()).saturating_add(score),
|
||||
path,
|
||||
)
|
||||
})
|
||||
.min()
|
||||
.unwrap_or(u16::MAX);
|
||||
.unwrap_or((u16::MAX, Vec::new()));
|
||||
|
||||
min_lengths.insert(remaining, min_length);
|
||||
min_length
|
||||
bests.insert(remaining, best.clone());
|
||||
best
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue