Update solution for 2022 day 02

This commit is contained in:
Patrick Auernig 2022-12-02 16:58:46 +01:00
parent 3c9a208ba6
commit 8e9b63cfc3

View File

@ -6,28 +6,23 @@ use std::{
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Player { pub enum Player {
Rock, Rock = 1,
Paper, Paper = 2,
Scissors, Scissors = 3,
} }
impl Player { impl Player {
pub fn shape_score(&self) -> u64 {
match self {
Self::Rock => 1,
Self::Paper => 2,
Self::Scissors => 3,
}
}
pub fn wins_against(&self, other: &Self) -> RoundResult { pub fn wins_against(&self, other: &Self) -> RoundResult {
use Player::*;
use RoundResult::*;
match (self, other) { match (self, other) {
(Self::Rock, Self::Paper) => RoundResult::Loss, (Rock, Paper) => Loss,
(Self::Rock, Self::Scissors) => RoundResult::Win, (Rock, Scissors) => Win,
(Self::Paper, Self::Rock) => RoundResult::Win, (Paper, Rock) => Win,
(Self::Paper, Self::Scissors) => RoundResult::Loss, (Paper, Scissors) => Loss,
(Self::Scissors, Self::Rock) => RoundResult::Loss, (Scissors, Rock) => Loss,
(Self::Scissors, Self::Paper) => RoundResult::Win, (Scissors, Paper) => Win,
_ => RoundResult::Draw, _ => RoundResult::Draw,
} }
} }
@ -59,9 +54,9 @@ impl Player {
#[derive(Debug)] #[derive(Debug)]
pub enum RoundResult { pub enum RoundResult {
Win, Loss = 0,
Draw, Draw = 3,
Loss, Win = 6,
} }
impl RoundResult { impl RoundResult {
@ -76,12 +71,7 @@ impl RoundResult {
} }
pub fn calculate_round_score(player1: &Player, player2: &Player) -> u64 { pub fn calculate_round_score(player1: &Player, player2: &Player) -> u64 {
player1.shape_score() (*player1 as u64) + (player1.wins_against(&player2) as u64)
+ match player1.wins_against(&player2) {
RoundResult::Win => 6,
RoundResult::Draw => 3,
RoundResult::Loss => 0,
}
} }
pub fn parse_lines<P>(path: P) -> io::Result<impl Iterator<Item = (String, String)>> pub fn parse_lines<P>(path: P) -> io::Result<impl Iterator<Item = (String, String)>>