From 86eb94514e9751617ff903a11393e2d89a39bafb Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Fri, 2 Dec 2022 16:11:46 -0800 Subject: [PATCH] day 2 --- 2022/Cargo.toml | 4 ++ 2022/src/day1.rs | 16 ------- 2022/src/day2.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 2022/src/day2.rs diff --git a/2022/Cargo.toml b/2022/Cargo.toml index a067952..dd3d9c5 100644 --- a/2022/Cargo.toml +++ b/2022/Cargo.toml @@ -11,6 +11,10 @@ path = "src/lib.rs" name = "day1" path = "src/day1.rs" +[[bin]] +name = "day2" +path = "src/day2.rs" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/2022/src/day1.rs b/2022/src/day1.rs index 41086a8..96dd8e6 100644 --- a/2022/src/day1.rs +++ b/2022/src/day1.rs @@ -1,21 +1,5 @@ const DATA: &'static str = include_str!("../data/day1.txt"); -// const DATA: &'static str = "1000 -// 2000 -// 3000 - -// 4000 - -// 5000 -// 6000 - -// 7000 -// 8000 -// 9000 - -// 10000 -// "; - fn main() { let mut counts = DATA.trim() diff --git a/2022/src/day2.rs b/2022/src/day2.rs new file mode 100644 index 0000000..400c623 --- /dev/null +++ b/2022/src/day2.rs @@ -0,0 +1,108 @@ +use std::str::FromStr; + + +const DATA: &'static str = include_str!("../data/day2.txt"); +// const DATA: &'static str = " +// A Y +// B X +// C Z +// "; + + +enum RPS { + Rock = 1, + Paper = 2, + Scissors = 3, +} +impl FromStr for RPS { + type Err = (); + fn from_str(s: &str) -> Result { + match s { + "A" | "X" => Ok(Rock), + "B" | "Y" => Ok(Paper), + "C" | "Z" => Ok(Scissors), + _ => Err(()) + } + } +} + + +enum Outcome { + Win = 6, + Loss = 0, + Draw = 3, +} +impl FromStr for Outcome { + type Err = (); + fn from_str(s: &str) -> Result { + match s { + "X" => Ok(Loss), + "Y" => Ok(Draw), + "Z" => Ok(Win), + _ => Err(()) + } + } +} + +use RPS::*; +use Outcome::*; + + +fn compare(me: &RPS, them: &RPS) -> Outcome { + match (me, them) { + (Rock, Rock) => Draw, + (Rock, Paper) => Loss, + (Rock, Scissors) => Win, + (Paper, Rock) => Win, + (Paper, Paper) => Draw, + (Paper, Scissors) => Loss, + (Scissors, Rock) => Loss, + (Scissors, Paper) => Win, + (Scissors, Scissors) => Draw, + } +} + + +fn get_response(opponent: &RPS, outcome: &Outcome) -> RPS { + match (opponent, outcome) { + (Rock, Win) => Paper, + (Rock, Loss) => Scissors, + (Rock, Draw) => Rock, + (Paper, Win) => Scissors, + (Paper, Loss) => Rock, + (Paper, Draw) => Paper, + (Scissors, Win) => Rock, + (Scissors, Loss) => Paper, + (Scissors, Draw) => Scissors, + } +} + + +fn score(me: RPS, them: RPS) -> u64 { + let outcome = compare(&me, &them); + (me as u64) + (outcome as u64) +} + + +fn main() { + let mut total_1 = 0; + for pair in DATA.trim().split("\n") { + let mut it = pair.split(" ") + .map(|v| v.parse::().unwrap()); + let (them, me) = (it.next().unwrap(), it.next().unwrap()); + total_1 += score(me, them); + } + + println!("Part 1: {total_1}"); + + let mut total_2 = 0; + for pair in DATA.trim().split("\n") { + let mut it = pair.split(" "); + let them = it.next().unwrap().parse::().unwrap(); + let outcome = it.next().unwrap().parse::().unwrap(); + let me = get_response(&them, &outcome); + total_2 += me as u64; + total_2 += outcome as u64 + } + println!("Part 2: {total_2}"); +}