commit cf5dbf3d2aeea5efa8d435bf80b703a55497cb92 Author: Jeff Date: Mon Sep 18 08:32:14 2023 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..645efb8 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2468b66 --- /dev/null +++ b/Cargo.toml @@ -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] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e663dbd --- /dev/null +++ b/src/main.rs @@ -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, + suspects: BTreeSet, + weapons: BTreeSet, +} + +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 + } + ) + } +}