Last active
September 16, 2019 14:57
-
-
Save pravic/8747be9bc926ac3819647df439f27b8f to your computer and use it in GitHub Desktop.
scrabble finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const WORDS: &str = include_str!("../words.txt"); | |
fn main() { | |
let pattern = std::env::args() | |
.nth(1) | |
.expect("usage: words <pattern> [known letters]") | |
.to_ascii_lowercase() | |
.replace(" ", "") | |
; | |
let known = std::env::args() | |
.skip(1) | |
.map(|s| s.chars().filter(|&c| c != '?' && c != '_' && !c.is_whitespace()).collect::<String>()) | |
.collect::<Vec<_>>() | |
; | |
let known: String = known.join("").to_ascii_lowercase(); | |
eprintln!("known letters: {:?}", known); | |
let mut res = Vec::new(); | |
'O: for word in WORDS.lines() { | |
if word.len() == pattern.len() { | |
// "?a??ern" | |
let letters = word.as_bytes(); | |
let pattern = pattern.as_bytes(); | |
let known = known.as_bytes(); | |
for i in 0..letters.len() { | |
let p = pattern[i]; | |
let w = letters[i].to_ascii_lowercase(); | |
if p == b'?' || p == b'_' { | |
// the masked letter should not by one of the present letters | |
if known.iter().any(|&c| c == w) { | |
continue 'O; | |
} | |
} else if p != w { | |
continue 'O; | |
} | |
} | |
res.push(word); | |
} | |
} | |
eprintln!("{} results for {:?} among {} words", res.len(), pattern, WORDS.lines().count()); | |
for w in res.iter().take(20) { | |
println!("{}", w); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment