From cd25edb1ded40bd5bf328b928dc533c53cecaa05 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 2 Dec 2022 19:32:49 +0100 Subject: [AoC2022] Rust 02-01 --- AoC2022/02/solver.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 AoC2022/02/solver.rs (limited to 'AoC2022') diff --git a/AoC2022/02/solver.rs b/AoC2022/02/solver.rs new file mode 100644 index 0000000..1f9a394 --- /dev/null +++ b/AoC2022/02/solver.rs @@ -0,0 +1,52 @@ +use std::fs; +use std::io::{self, BufRead}; + +#[derive(Debug, PartialEq)] +enum Hand { + Rock, + Paper, + Scissors, +} + +fn translate(play: &str) -> Hand { + match play { + "A" | "X" => Hand::Rock, + "B" | "Y" => Hand::Paper, + "C" | "Z" => Hand::Scissors, + &_ => panic!(), + } +} + +fn weight(play: Hand) -> u32 { + match play { + Hand::Rock => 1, + Hand::Paper => 2, + Hand::Scissors => 3, + } +} + +fn fight(my: Hand, other: Hand) -> u32 { + match (my, other) { + (a, b) if a == b => 3, + (Hand::Rock, Hand::Scissors) + | (Hand::Scissors, Hand::Paper) + | (Hand::Paper, Hand::Rock) => 6, + _ => 0, + } +} + +fn main() -> std::io::Result<()> { + let file = fs::File::open("input").unwrap(); + let lines = io::BufReader::new(file).lines(); + let mut score = 0; + for line in lines { + if let [ot, my] = line?.split_whitespace().collect::>()[..] { + score += fight(translate(my), translate(ot)) + weight(translate(my)); + } else { + todo!() + }; + } + + println!("{}", score); + Ok(()) +} -- cgit v1.2.3