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::{
|
use std::{
|
||||||
collections::{HashMap, hash_map::Entry},
|
collections::HashMap,
|
||||||
io::{self, BufRead, BufReader},
|
io::{self, BufRead, BufReader},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -13,38 +13,43 @@ fn main() {
|
||||||
.collect::<Result<_, _>>()
|
.collect::<Result<_, _>>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut min_lengths: HashMap<LetterSet, u16> = HashMap::new();
|
let mut bests = HashMap::new();
|
||||||
let min_length = solve(dictionary.as_slice(), &mut min_lengths, LetterSet::FULL);
|
let best = solve(dictionary.as_slice(), &mut bests, LetterSet::FULL);
|
||||||
println!("{min_length}");
|
println!("{best:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// length doesn't include spaces. i think that's okay
|
// length doesn't include spaces. i think that's okay
|
||||||
fn solve(
|
fn solve(
|
||||||
dictionary: &[String],
|
dictionary: &[String],
|
||||||
min_lengths: &mut HashMap<LetterSet, u16>,
|
bests: &mut HashMap<LetterSet, (u16, Vec<String>)>,
|
||||||
remaining: LetterSet,
|
remaining: LetterSet,
|
||||||
) -> u16 {
|
) -> (u16, Vec<String>) {
|
||||||
if remaining.is_empty() {
|
if remaining.is_empty() {
|
||||||
return 0;
|
return (0, Vec::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(min) = min_lengths.get(&remaining) {
|
if let Some(min) = bests.get(&remaining) {
|
||||||
return *min;
|
return min.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
let min_length = dictionary
|
let best = dictionary
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|word| remaining != remaining.difference(LetterSet::from(word.as_bytes())))
|
.filter(|word| remaining != remaining.difference(LetterSet::from(word.as_bytes())))
|
||||||
.map(|word| {
|
.map(|word| {
|
||||||
(u16::try_from(word.len()).unwrap()).saturating_add(solve(
|
let (score, mut path) = solve(
|
||||||
dictionary,
|
dictionary,
|
||||||
min_lengths,
|
bests,
|
||||||
remaining.difference(LetterSet::from(word)),
|
remaining.difference(LetterSet::from(word)),
|
||||||
))
|
);
|
||||||
|
path.push(word.clone());
|
||||||
|
(
|
||||||
|
(u16::try_from(word.len()).unwrap()).saturating_add(score),
|
||||||
|
path,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.min()
|
.min()
|
||||||
.unwrap_or(u16::MAX);
|
.unwrap_or((u16::MAX, Vec::new()));
|
||||||
|
|
||||||
min_lengths.insert(remaining, min_length);
|
bests.insert(remaining, best.clone());
|
||||||
min_length
|
best
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue