From 53afd7552796b70c3040626ca30479b7e3588bcf Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 4 Feb 2022 01:34:00 +1030 Subject: [PATCH] Initial commit --- Cargo.toml | 9 +++++++++ src/main.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..df70ca0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "wordle-rs" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..da09a45 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,55 @@ +use std::fs; +use std::collections::HashMap; +use regex::Regex; + +fn load_dictionary(filename: &str) -> Vec { + println!("Loading dictionary at {}", filename); + let rawfile = fs::read_to_string(filename).unwrap(); + let rawwords = rawfile.split('\n'); + let mut words = Vec::new(); + let re = Regex::new(r"^\w{5}$").unwrap(); + for line in rawwords { + if re.is_match(line) { + words.push(line.to_uppercase()); + } + } + words +} + +fn inc_char(c: char) -> char { + (c as u8 + 1) as char +} + +fn _generate_wordcache_nested(cache: &mut HashMap>, subcache: &[String], key: &str, depth: u8) { + for c in inc_char(key.chars().last().unwrap())..='Z' { + let sc2: Vec = subcache.iter().filter(|w| w.contains(c)).cloned().collect(); + if !sc2.is_empty() { + let key2 = format!("{}{}", key, c); + if depth > 0 { + _generate_wordcache_nested(cache, &sc2, &key2, depth-1); + } + cache.insert(key2, sc2); + } + } +} + +fn generate_wordcache(words: Vec) -> HashMap> { + let mut cache = HashMap::new(); + for c1 in 'A'..='Z' { + let sc: Vec = words.iter().filter(|w| w.contains(c1)).cloned().collect(); + if !sc.is_empty() { + let key = format!("{}", c1); + _generate_wordcache_nested(&mut cache, &sc, &key, 4); + cache.insert(key, sc); + } + } + cache.insert("".to_string(), words); + cache +} + +fn main() { + let words = load_dictionary("/usr/share/dict/words"); + println!("Hello, world! {} words in dict", words.len()); + let wordcache = generate_wordcache(words); + println!("{:?}", wordcache.keys()); +}