r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

3

u/mrwinkle Dec 02 '22

Rust

use std::fs;

#[derive(Debug, PartialEq, Copy, Clone)]
enum Shape {
    Rock = 1,
    Paper = 2,
    Scissors = 3,
}

impl Shape {
    fn from_str(c: &str) -> Self {
        match c {
            "A" | "X" => Shape::Rock,
            "B" | "Y" => Shape::Paper,
            "C" | "Z" => Shape::Scissors,
            _ => panic!("Unknown shape"),
        }
    }

    fn superior(&self) -> Self {
        match self {
            Self::Rock => Self::Paper,
            Self::Paper => Self::Scissors,
            Self::Scissors => Self::Rock,
        }
    }

    fn inferior(&self) -> Self {
        match self {
            Self::Rock => Self::Scissors,
            Self::Paper => Self::Rock,
            Self::Scissors => Self::Paper,
        }
    }
}

enum Strategy {
    Lose,
    Draw,
    Win,
}

impl Strategy {
    fn from_str(c: &str) -> Self {
        match c {
            "X" => Self::Lose,
            "Y" => Self::Draw,
            "Z" => Self::Win,
            _ => panic!("Unknown strategy"),
        }
    }
}

#[derive(Debug)]
struct Match {
    player: Shape,
    opponent: Shape,
}

impl Match {
    fn from_strategy(opponent: Shape, strategy: Strategy) -> Self {
        Self {
            opponent: opponent,
            player: match strategy {
                Strategy::Lose => opponent.inferior(),
                Strategy::Draw => opponent,
                Strategy::Win => opponent.superior(),
            },
        }
    }

    fn player_points(&self) -> u8 {
        let shape = self.player as u8;
        let outcome = {
            if self.player == self.opponent {
                3
            } else if self.player.inferior() == self.opponent {
                6
            } else {
                0
            }
        };

        shape + outcome
    }
}

fn parse_input(input: &str) -> Vec<Vec<&str>> {
    input.lines().map(|l| l.split(" ").collect()).collect()
}

fn part1(input: &str) -> u32 {
    let matches: Vec<Match> = parse_input(input)
        .iter()
        .map(|x| Match {
            player: Shape::from_str(x[1]),
            opponent: Shape::from_str(x[0]),
        })
        .collect();
    matches.iter().map(|m| m.player_points() as u32).sum()
}

fn part2(input: &str) -> u32 {
    let matches: Vec<Match> = parse_input(input)
        .iter()
        .map(|x| Match::from_strategy(Shape::from_str(x[0]), Strategy::from_str(x[1])))
        .collect();
    matches.iter().map(|m| m.player_points() as u32).sum()
}

fn main() {
    let input = fs::read_to_string("input/day2.txt").unwrap();
    println!("Part 1: {}", part1(&input));
    println!("Part 2: {}", part2(&input));
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_part1_example() {
        let example = "A Y\nB X\nC Z";
        assert_eq!(part1(&example), 15);
    }

    #[test]
    fn test_part1() {
        let input = fs::read_to_string("input/day2.txt").unwrap();
        assert_eq!(part1(&input), 11603);
    }

    #[test]
    fn test_part2_example() {
        let example = "A Y\nB X\nC Z";
        assert_eq!(part2(&example), 12);
    }

    #[test]
    fn test_part2() {
        let input = fs::read_to_string("input/day2.txt").unwrap();
        assert_eq!(part2(&input), 12725);
    }
}

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.