-
-
Save Leko/125e92a263043debc36f5aa895bfd015 to your computer and use it in GitHub Desktop.
Wordle solver Rust example
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
target |
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
# This file is automatically @generated by Cargo. | |
# It is not intended for manual editing. | |
version = 3 | |
[[package]] | |
name = "leko-wordle-solver-example" | |
version = "0.1.0" |
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
[package] | |
# You can use any package name | |
name = "leko-wordle-solver-example" | |
description = "A reference implementation of Wordle solver" | |
license = "MIT" | |
version = "0.1.0" | |
edition = "2021" | |
[[bin]] | |
# bin.name must be "wordle-solver" | |
name = "wordle-solver" | |
path = "main.rs" |
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
use std::io; | |
#[derive(Debug)] | |
enum Response { | |
Absent, | |
Present, | |
Correct, | |
} | |
#[derive(Debug)] | |
struct History { | |
word: Vec<char>, | |
response: Vec<Response>, | |
} | |
fn guess(history: &Vec<History>) -> &'static str { | |
// Implement your resolver! | |
"count" | |
} | |
fn main() { | |
let mut history: Vec<History> = Vec::new(); | |
let mut word = guess(&history); | |
println!("{}", word); | |
loop { | |
let mut line = String::new(); | |
io::stdin() | |
.read_line(&mut line) | |
.expect("Failed to read line"); | |
eprintln!("line: {}", line); | |
match line.as_str().trim() { | |
"NOT_IN_WORD_LIST" => { | |
panic!("You need to give another word") | |
} | |
_ => { | |
let response: Vec<Response> = line | |
.trim() | |
.split(",") | |
.map(|res| match res { | |
"correct" => Response::Correct, | |
"present" => Response::Present, | |
"absent" => Response::Absent, | |
unknown => panic!("Unrecognized response: {:?}", unknown), | |
}) | |
.collect(); | |
if response.iter().all(|r| matches!(r, Response::Correct)) { | |
eprintln!("win: {:?}", word); | |
break; | |
} | |
history.push(History { | |
word: word.chars().collect(), | |
response, | |
}); | |
eprintln!("{:?}", &history); | |
word = guess(&history); | |
println!("{}", word); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment