From 5d6d91f075e4cebf1406fc950ad200f5671f8898 Mon Sep 17 00:00:00 2001 From: Codinget Date: Fri, 2 Dec 2022 22:15:29 +0000 Subject: [PATCH] day 2 --- d2/.gitignore | 1 + d2/Cargo.lock | 7 ++++ d2/Cargo.toml | 8 +++++ d2/src/main.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 d2/.gitignore create mode 100644 d2/Cargo.lock create mode 100644 d2/Cargo.toml create mode 100644 d2/src/main.rs diff --git a/d2/.gitignore b/d2/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/d2/.gitignore @@ -0,0 +1 @@ +/target diff --git a/d2/Cargo.lock b/d2/Cargo.lock new file mode 100644 index 0000000..0a330d8 --- /dev/null +++ b/d2/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "d2" +version = "0.1.0" diff --git a/d2/Cargo.toml b/d2/Cargo.toml new file mode 100644 index 0000000..d140e99 --- /dev/null +++ b/d2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d2" +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/d2/src/main.rs b/d2/src/main.rs new file mode 100644 index 0000000..17382fe --- /dev/null +++ b/d2/src/main.rs @@ -0,0 +1,89 @@ +use std::fs::read_to_string; +use std::io::Error; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum RPS { + Rock, + Paper, + Scissor, +} +use RPS::*; + +impl RPS { + fn from_char(c: char) -> Self { + match c { + 'A' | 'X' => Rock, + 'B' | 'Y' => Paper, + 'C' | 'Z' => Scissor, + _ => panic!() + } + } + pub fn score(self) -> u32 { + match self { + Rock => 1, + Paper => 2, + Scissor => 3, + } + } + pub fn outcome(opponent: Self, you: Self) -> Outcome { + match (you, opponent) { + (Rock, Rock) | (Paper, Paper) | (Scissor, Scissor) => Draw, + (Rock, Scissor) | (Paper, Rock) | (Scissor, Paper) => Win, + (Rock, Paper) | (Paper, Scissor) | (Scissor, Rock) => Loss, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Outcome { + Win, + Loss, + Draw, +} +use Outcome::*; + +impl Outcome { + pub fn from_char(c: char) -> Self { + match c { + 'X' => Loss, + 'Y' => Draw, + 'Z' => Win, + _ => panic!() + } + } + + pub fn score(self) -> u32 { + match self { + Win => 6, + Draw => 3, + Loss => 0 + } + } + + pub fn for_opponent(self, opponent: RPS) -> RPS { + match (self, opponent) { + (Win, Rock) | (Draw, Paper) | (Loss, Scissor) => Paper, + (Win, Paper) | (Draw, Scissor) | (Loss, Rock) => Scissor, + (Win, Scissor) | (Draw, Rock) | (Loss, Paper) => Rock, + } + } +} + +fn main() -> Result<(), Error> { + let mut total_score_p1 = 0; + let mut total_score_p2 = 0; + for line in read_to_string("input.txt")?.split("\n") { + if line.is_empty() { continue } + let (opponent, you) = (line.chars().nth(0).unwrap(), line.chars().nth(2).unwrap()); + let (opponent, your_move, outcome) = (RPS::from_char(opponent), RPS::from_char(you), Outcome::from_char(you)); + let score_p1 = your_move.score() + RPS::outcome(opponent, your_move).score(); + total_score_p1 += score_p1; + let score_p2 = outcome.score() + outcome.for_opponent(opponent).score(); + total_score_p2 += score_p2; + } + + println!("part 1: {}", total_score_p1); + println!("part 2: {}", total_score_p2); + + Ok(()) +}