Initial commit

This commit is contained in:
Jeff 2023-09-18 08:32:14 -04:00
commit cf5dbf3d2a
4 changed files with 126 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "clue_solver"
version = "0.1.0"

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "clue_solver"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

110
src/main.rs Normal file
View File

@ -0,0 +1,110 @@
use std::collections::BTreeSet;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Guess {
room: Room,
suspect: Suspect,
weapon: Weapon,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Clue {
Room(Room),
Suspect(Suspect),
Weapon(Weapon),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Room {
Library,
Kitchen,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Suspect {
Green,
White,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Weapon {
Rope,
LeadPipe,
}
#[derive(Clone, Debug)]
pub struct Solver {
rooms: BTreeSet<Room>,
suspects: BTreeSet<Suspect>,
weapons: BTreeSet<Weapon>,
}
impl Solver {
pub fn new() -> Self {
let mut rooms = BTreeSet::new();
rooms.insert(Room::Library);
rooms.insert(Room::Kitchen);
let mut suspects = BTreeSet::new();
suspects.insert(Suspect::Green);
suspects.insert(Suspect::White);
let mut weapons = BTreeSet::new();
weapons.insert(Weapon::Rope);
weapons.insert(Weapon::LeadPipe);
Self {
rooms,
suspects,
weapons,
}
}
pub fn guess(&self) -> Guess {
Guess {
room: *self.rooms.first().unwrap(),
suspect: *self.suspects.first().unwrap(),
weapon: *self.weapons.first().unwrap(),
}
}
pub fn eliminate(&mut self, clue: Clue) -> Result<(), String> {
match clue {
Clue::Room(room_clue) => self.rooms.remove(&room_clue),
Clue::Suspect(suspect_clue) => self.suspects.remove(&suspect_clue),
Clue::Weapon(weapon_clue) => self.weapons.remove(&weapon_clue),
};
Ok(())
}
}
fn main() {
println!("Hello, world!");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn solve() {
let mut solver = Solver::new();
solver.eliminate(Clue::Room(Room::Library)).unwrap();
solver.eliminate(Clue::Suspect(Suspect::Green)).unwrap();
solver.eliminate(Clue::Weapon(Weapon::Rope)).unwrap();
assert_eq!(
solver.guess(),
Guess {
room: Room::Kitchen,
suspect: Suspect::White,
weapon: Weapon::LeadPipe
}
)
}
}