diff --git a/src/main.rs b/src/main.rs index 04d57c2..b65462d 100644 --- a/src/main.rs +++ b/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::>() .unwrap(); - let mut min_lengths: HashMap = 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, + bests: &mut HashMap)>, remaining: LetterSet, -) -> u16 { +) -> (u16, Vec) { 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 }