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