diff --git a/rust/day2/src/main.rs b/rust/day2/src/main.rs index 1e9830f..aa32241 100644 --- a/rust/day2/src/main.rs +++ b/rust/day2/src/main.rs @@ -1,22 +1,48 @@ -use std::fs::read_to_string; use std::collections::HashMap; +use std::fs::read_to_string; mod rps; -fn main() -{ - // Checking if the file is found. - let mut score : u64 = 0; - let mut oponent : Vec = read_to_string("input").unwrap().split_whitespace().map(|s| s.to_string()).collect(); - let mut player = oponent.clone(); - oponent.retain(|s| s == "A" || s == "B" || s == "C"); - player.retain(|s| s == "X" || s == "Y" || s == "Z"); - assert!(oponent.len() == player.len(), "Oponent and Player don't play as many turns."); - - let converter : HashMap = rps::get_converter(); +fn first_strategy(player: &mut Vec, oponent: &Vec) { + let mut score: u64 = 0; + let converter: HashMap = rps::get_converter(); for tools in oponent.iter().zip(player.iter_mut()) { let o = tools.0.char_indices().next().unwrap().1; let p = tools.1.char_indices().next().unwrap().1; - score += converter.get(&p).unwrap().battle(converter.get(&o).unwrap()); + score += converter + .get(&p) + .unwrap() + .battle(converter.get(&o).unwrap()); } - println!("Final score: {}", score); -} \ No newline at end of file + println!("Final score (first method): {}", score); +} + +fn second_strategy(player: &mut Vec, oponent: &Vec) { + let mut score: u64 = 0; + let converter: HashMap = rps::get_converter(); + for tools in oponent.iter().zip(player.iter_mut()) { + let o = tools.0.char_indices().next().unwrap().1; + let oponent_tool = converter.get(&o).unwrap(); + let player_tool = + rps::get_converter_second(tools.1.char_indices().next().unwrap().1, *oponent_tool) + .unwrap(); + score += player_tool.battle(oponent_tool); + } + println!("Final score (second method): {}", score); +} +fn main() { + // Checking if the file is found. + let mut oponent: Vec = read_to_string("input") + .unwrap() + .split_whitespace() + .map(|s| s.to_string()) + .collect(); + let mut player = oponent.clone(); + oponent.retain(|s| s == "A" || s == "B" || s == "C"); + player.retain(|s| s == "X" || s == "Y" || s == "Z"); + assert!( + oponent.len() == player.len(), + "Oponent and Player don't play as many turns." + ); + first_strategy(&mut player, &oponent); + second_strategy(&mut player, &oponent); +} diff --git a/rust/day2/src/rps.rs b/rust/day2/src/rps.rs index 453251c..8bf9d62 100644 --- a/rust/day2/src/rps.rs +++ b/rust/day2/src/rps.rs @@ -1,19 +1,27 @@ use std::collections::HashMap; -#[derive(Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub enum RockPaperScissors { Rock = 0, Paper = 1, - Scissors = 2 + Scissors = 2, } -impl RockPaperScissors{ - pub fn battle(&self, other: &RockPaperScissors) -> u64 - { +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 from_usize(value: usize) -> Option { + match value { + 0 => Some(RockPaperScissors::Rock), + 1 => Some(RockPaperScissors::Paper), + 2 => Some(RockPaperScissors::Scissors), + _ => None + } + } } pub fn get_converter() -> HashMap { @@ -27,10 +35,35 @@ pub fn get_converter() -> HashMap { ]) } +pub fn get_converter_second(ch: char, oponent: RockPaperScissors) -> Option { + let binding = get_weight(); + let weight = binding.get(&oponent).unwrap(); + match ch { + 'X' => { + let indice = weight + .iter() + .enumerate() + .max_by(|(_, a), (_, b)| a.cmp(b)) + .map(|(index, _)| index); + RockPaperScissors::from_usize(indice.unwrap()) + } + 'Y' => Some(oponent), + 'Z' => { + let indice = weight + .iter() + .enumerate() + .min_by(|(_, a), (_, b)| a.cmp(b)) + .map(|(index, _)| index); + RockPaperScissors::from_usize(indice.unwrap()) + } + _ => None, + } +} + fn get_weight() -> HashMap { HashMap::from([ - (RockPaperScissors::Rock, [3,0,6]), - (RockPaperScissors::Paper, [6,3,0]), - (RockPaperScissors::Scissors, [0,6,3]), + (RockPaperScissors::Rock, [3, 0, 6]), + (RockPaperScissors::Paper, [6, 3, 0]), + (RockPaperScissors::Scissors, [0, 6, 3]), ]) -} \ No newline at end of file +}