Last active
January 17, 2022 13:36
-
-
Save wperron/dbb5408bdd3059366fc3cd4599bb4ba3 to your computer and use it in GitHub Desktop.
Wordle validator
This file contains hidden or 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
use std::fmt::Display; | |
#[derive(PartialEq, Eq, Debug)] | |
enum Result { | |
Absent, | |
OutOfPlace, | |
Correct, | |
OutOfBounds, | |
} | |
impl Display for Result { | |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
match self { | |
Result::Absent => write!(f, "⬛"), | |
Result::OutOfPlace => write!(f, "🟨"), | |
Result::Correct => write!(f, "🟩"), | |
Result::OutOfBounds => write!(f, "❌"), | |
} | |
} | |
} | |
#[derive(PartialEq, Eq, Debug)] | |
struct Guess { | |
inner: Vec<Result>, | |
} | |
impl Guess { | |
fn compare(a: String, b: String) -> Self { | |
let mut res = vec![]; | |
let mut a_chars = a.chars(); | |
for c in b.chars() { | |
let maybe_next = a_chars.next(); | |
match maybe_next { | |
None => res.push(Result::OutOfBounds), | |
Some(same_pos) => { | |
if c == same_pos { | |
res.push(Result::Correct); | |
} else if a.contains(c) { | |
res.push(Result::OutOfPlace); | |
} else { | |
res.push(Result::Absent); | |
} | |
} | |
} | |
} | |
Self { inner: res } | |
} | |
} | |
impl Display for Guess { | |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
for r in &self.inner { | |
write!(f, "{}", r)?; | |
} | |
Ok(()) | |
} | |
} | |
struct Wordle { | |
word: String, | |
} | |
impl Wordle { | |
fn guess(&self, guess: String) -> Guess { | |
Guess::compare(self.word.clone(), guess) | |
} | |
} | |
impl From<String> for Wordle { | |
fn from(word: String) -> Self { | |
Self { word } | |
} | |
} | |
fn main() { | |
println!("Hello, world!"); | |
} | |
#[cfg(test)] | |
mod test { | |
use super::*; | |
#[test] | |
fn test_wordle() { | |
let wordle = Wordle::from(String::from("fudge")); | |
assert_eq!( | |
wordle.guess(String::from("reads")), | |
Guess { | |
inner: vec![ | |
Result::Absent, | |
Result::OutOfPlace, | |
Result::Absent, | |
Result::OutOfPlace, | |
Result::Absent, | |
] | |
} | |
); | |
assert_eq!( | |
wordle.guess(String::from("lodge")), | |
Guess { | |
inner: vec![ | |
Result::Absent, | |
Result::Absent, | |
Result::Correct, | |
Result::Correct, | |
Result::Correct, | |
] | |
} | |
); | |
} | |
#[test] | |
fn test_doubles() { | |
let wordle = Wordle::from(String::from("sassy")); | |
assert_eq!( | |
wordle.guess(String::from("space")), | |
Guess { | |
inner: vec![ | |
Result::Correct, | |
Result::Absent, | |
Result::OutOfPlace, | |
Result::Absent, | |
Result::Absent, | |
] | |
} | |
); | |
} | |
#[test] | |
fn test_oob() { | |
let wordle = Wordle::from(String::from("fudge")); | |
assert_eq!( | |
wordle.guess(String::from("lodging")), | |
Guess { | |
inner: vec![ | |
Result::Absent, | |
Result::Absent, | |
Result::Correct, | |
Result::Correct, | |
Result::Absent, | |
Result::OutOfBounds, | |
Result::OutOfBounds, | |
] | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment