From 56eb362c3d250bc7f76c70c4e6aee6719b42583b Mon Sep 17 00:00:00 2001 From: Xiretza Date: Fri, 2 Dec 2022 07:36:18 +0100 Subject: [PATCH] 2022 day2/rust: add solution --- 2022/day2/rust/Cargo.toml | 8 +++ 2022/day2/rust/src/main.rs | 121 +++++++++++++++++++++++++++++++++++++ Cargo.lock | 4 ++ Cargo.toml | 1 + 4 files changed, 134 insertions(+) create mode 100644 2022/day2/rust/Cargo.toml create mode 100644 2022/day2/rust/src/main.rs diff --git a/2022/day2/rust/Cargo.toml b/2022/day2/rust/Cargo.toml new file mode 100644 index 0000000..cb7d5b8 --- /dev/null +++ b/2022/day2/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust_2022_02" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2022/day2/rust/src/main.rs b/2022/day2/rust/src/main.rs new file mode 100644 index 0000000..c2a2a99 --- /dev/null +++ b/2022/day2/rust/src/main.rs @@ -0,0 +1,121 @@ +use std::{ + io::{stdin, Read}, + str::FromStr, +}; + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +enum Rps { + Rock, + Paper, + Scissors, +} + +impl Rps { + fn outcome_against(&self, other: Rps) -> Outcome { + match (self, other) { + (Rps::Rock, Rps::Scissors) | (Rps::Paper, Rps::Rock) | (Rps::Scissors, Rps::Paper) => { + Outcome::Win + } + (Rps::Rock, Rps::Rock) | (Rps::Paper, Rps::Paper) | (Rps::Scissors, Rps::Scissors) => { + Outcome::Draw + } + (Rps::Rock, Rps::Paper) | (Rps::Paper, Rps::Scissors) | (Rps::Scissors, Rps::Rock) => { + Outcome::Lose + } + } + } + + fn score(&self) -> u64 { + match self { + Rps::Rock => 1, + Rps::Paper => 2, + Rps::Scissors => 3, + } + } + + fn with_outcome(&self, outcome: Outcome) -> Rps { + match (self, outcome) { + (r, Outcome::Draw) => *r, + + (Rps::Rock, Outcome::Lose) => Rps::Scissors, + (Rps::Paper, Outcome::Lose) => Rps::Rock, + (Rps::Scissors, Outcome::Lose) => Rps::Paper, + + (Rps::Rock, Outcome::Win) => Rps::Paper, + (Rps::Paper, Outcome::Win) => Rps::Scissors, + (Rps::Scissors, Outcome::Win) => Rps::Rock, + } + } +} + +impl FromStr for Rps { + type Err = (); + + fn from_str(s: &str) -> Result { + match s { + "A" | "X" => Ok(Rps::Rock), + "B" | "Y" => Ok(Rps::Paper), + "C" | "Z" => Ok(Rps::Scissors), + _ => Err(()), + } + } +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +enum Outcome { + Lose, + Draw, + Win, +} + +impl Outcome { + fn score(&self) -> u64 { + match self { + Outcome::Lose => 0, + Outcome::Draw => 3, + Outcome::Win => 6, + } + } +} + +impl FromStr for Outcome { + type Err = (); + + fn from_str(s: &str) -> Result { + match s { + "X" => Ok(Outcome::Lose), + "Y" => Ok(Outcome::Draw), + "Z" => Ok(Outcome::Win), + _ => Err(()), + } + } +} + +fn main() { + let mut data = String::new(); + stdin().read_to_string(&mut data).unwrap(); + + let mut sum1 = 0u64; + let mut sum2 = 0u64; + + for line in data.lines() { + let parts: Vec<_> = line.split_whitespace().collect(); + let other: Rps = parts[0].parse().unwrap(); + + { + let me: Rps = parts[1].parse().unwrap(); + let outcome = me.outcome_against(other); + + sum1 += me.score() + outcome.score(); + } + { + let outcome = parts[1].parse().unwrap(); + let me = other.with_outcome(outcome); + + sum2 += me.score() + outcome.score(); + } + } + + println!("{}", sum1); + println!("{}", sum2); +} diff --git a/Cargo.lock b/Cargo.lock index 2240a01..71b3503 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -465,6 +465,10 @@ dependencies = [ name = "rust_2022_01" version = "0.1.0" +[[package]] +name = "rust_2022_02" +version = "0.1.0" + [[package]] name = "ryu" version = "1.0.11" diff --git a/Cargo.toml b/Cargo.toml index 02324d7..e7ea83a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ members = [ "2021/day16/rust", "2021/day22/rust", "2022/day1/rust", + "2022/day2/rust", ]