diff --git a/rust/day2/src/main.rs b/rust/day2/src/main.rs index 2307fe5..8d525ec 100644 --- a/rust/day2/src/main.rs +++ b/rust/day2/src/main.rs @@ -1,10 +1,13 @@ -use crate::rps::get_converter; - use std::collections::HashMap; mod rps; fn main() { let converter : HashMap = rps::get_converter(); - println!("Hello world!"); + let player = converter.get(&'Y').unwrap().battle(converter.get(&'A').unwrap()); + println!("First battle = {}", player); + let player = converter.get(&'X').unwrap().battle(converter.get(&'B').unwrap()); + println!("Second battle = {}", player); + let player = converter.get(&'Z').unwrap().battle(converter.get(&'C').unwrap()); + println!("Third battle = {}", player); } \ No newline at end of file diff --git a/rust/day2/src/rps.rs b/rust/day2/src/rps.rs index 03ac721..453251c 100644 --- a/rust/day2/src/rps.rs +++ b/rust/day2/src/rps.rs @@ -1,9 +1,19 @@ use std::collections::HashMap; +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub enum RockPaperScissors { - Rock, - Paper, - Scissors + Rock = 0, + Paper = 1, + Scissors = 2 +} + +impl RockPaperScissors{ + pub fn battle(&self, other: &RockPaperScissors) -> u64 + { + let weight = get_weight(); + let tab = weight.get(&self).unwrap(); + (*self as u64) + 1 + (tab[*other as usize] as u64) + } } pub fn get_converter() -> HashMap { @@ -15,4 +25,12 @@ pub fn get_converter() -> HashMap { ('Y', RockPaperScissors::Paper), ('Z', RockPaperScissors::Scissors), ]) +} + +fn get_weight() -> HashMap { + HashMap::from([ + (RockPaperScissors::Rock, [3,0,6]), + (RockPaperScissors::Paper, [6,3,0]), + (RockPaperScissors::Scissors, [0,6,3]), + ]) } \ No newline at end of file