From 8e9b63cfc3710cd162a35ad2f9a43b7420ff1857 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Fri, 2 Dec 2022 16:58:46 +0100 Subject: [PATCH] Update solution for 2022 day 02 --- 2022/day-02/src/lib.rs | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/2022/day-02/src/lib.rs b/2022/day-02/src/lib.rs index 161c73f..9aba27d 100644 --- a/2022/day-02/src/lib.rs +++ b/2022/day-02/src/lib.rs @@ -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

(path: P) -> io::Result>