2022-12-13 14:20:19 +01:00
|
|
|
#[derive(PartialEq, Debug, Clone, Copy)]
|
2022-12-13 14:00:48 +01:00
|
|
|
pub enum Choice {
|
|
|
|
Rock,
|
|
|
|
Paper,
|
|
|
|
Scissors,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Choice {
|
|
|
|
pub fn score(&self) -> u32 {
|
|
|
|
match self {
|
|
|
|
Choice::Rock => 1,
|
|
|
|
Choice::Paper => 2,
|
|
|
|
Choice::Scissors => 3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:20:19 +01:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-12-13 14:00:48 +01:00
|
|
|
pub struct Round {
|
2022-12-13 14:20:19 +01:00
|
|
|
pub opponent: Choice,
|
|
|
|
pub own: Choice,
|
2022-12-13 14:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum RoundResult {
|
|
|
|
Win,
|
|
|
|
Draw,
|
|
|
|
Loss,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Round {
|
|
|
|
pub fn result(&self) -> RoundResult {
|
|
|
|
let win = match self.own {
|
|
|
|
Choice::Rock => Choice::Scissors,
|
|
|
|
Choice::Paper => Choice::Rock,
|
|
|
|
Choice::Scissors => Choice::Paper,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.opponent == win {
|
|
|
|
RoundResult::Win
|
|
|
|
} else if self.opponent == self.own {
|
|
|
|
RoundResult::Draw
|
|
|
|
} else {
|
|
|
|
RoundResult::Loss
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn score(&self) -> u32 {
|
|
|
|
let choice_score = self.own.score();
|
|
|
|
let result_score = match self.result() {
|
|
|
|
RoundResult::Win => 6,
|
|
|
|
RoundResult::Draw => 3,
|
|
|
|
RoundResult::Loss => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
choice_score + result_score
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:20:19 +01:00
|
|
|
pub fn parse_rounds(input: &String) -> Vec<(char, char)> {
|
2022-12-13 14:00:48 +01:00
|
|
|
let lines = input.lines();
|
|
|
|
let mut rounds = Vec::new();
|
|
|
|
|
|
|
|
for line in lines {
|
|
|
|
let (opponent, own) = line.split_once(' ').unwrap();
|
2022-12-13 14:20:19 +01:00
|
|
|
rounds.push((
|
|
|
|
opponent.chars().next().unwrap(),
|
|
|
|
own.chars().next().unwrap(),
|
|
|
|
));
|
2022-12-13 14:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rounds
|
|
|
|
}
|