Day 2 finished.
This commit is contained in:
parent
7ab07edd05
commit
e5237a7611
2 changed files with 83 additions and 24 deletions
|
@ -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<String> = 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<char, rps::RockPaperScissors> = rps::get_converter();
|
||||
fn first_strategy(player: &mut Vec<String>, oponent: &Vec<String>) {
|
||||
let mut score: u64 = 0;
|
||||
let converter: HashMap<char, rps::RockPaperScissors> = 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);
|
||||
}
|
||||
println!("Final score (first method): {}", score);
|
||||
}
|
||||
|
||||
fn second_strategy(player: &mut Vec<String>, oponent: &Vec<String>) {
|
||||
let mut score: u64 = 0;
|
||||
let converter: HashMap<char, rps::RockPaperScissors> = 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<String> = 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);
|
||||
}
|
||||
|
|
|
@ -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<RockPaperScissors> {
|
||||
match value {
|
||||
0 => Some(RockPaperScissors::Rock),
|
||||
1 => Some(RockPaperScissors::Paper),
|
||||
2 => Some(RockPaperScissors::Scissors),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_converter() -> HashMap<char, RockPaperScissors> {
|
||||
|
@ -27,10 +35,35 @@ pub fn get_converter() -> HashMap<char, RockPaperScissors> {
|
|||
])
|
||||
}
|
||||
|
||||
pub fn get_converter_second(ch: char, oponent: RockPaperScissors) -> Option<RockPaperScissors> {
|
||||
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<RockPaperScissors, [u8; 3]> {
|
||||
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]),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue