Sblog

AoC 2022 - Day 2

Date: 2022-12-02

I've created a new repo for the AoC 2022 here https://github.com/stygian91/advent-of-code-2022/tree/master/

Today's exercise was pretty easy again (you can find it here). The most fun part was modelling the problem with Rust's nice enums and match statements. I made a utility method that gives you the corresponding hand based on the player's hand and the desired result. I used this to find out which are the winning and losing hands so I can compare them in part 1. In part 2, I used the same method to instead find out which hand the player must choose in order to get the desired game outcome.

#[derive(Debug, Clone, PartialEq)]
pub enum Hand {
    Rock,
    Paper,
    Scissors,
}

impl Hand {
    pub fn get_corresponding_hand(&self, result: &GameResult) -> Hand {
        match result {
            GameResult::Loss => match self {
                Hand::Rock => Hand::Paper,
                Hand::Paper => Hand::Scissors,
                Hand::Scissors => Hand::Rock,
            },
            GameResult::Draw => self.clone(),
            GameResult::Win => match self {
                Hand::Rock => Hand::Scissors,
                Hand::Paper => Hand::Rock,
                Hand::Scissors => Hand::Paper,
            },
        }
    }
}