From f5f940c52b38a5e17092f5896dc2fc13e8443478 Mon Sep 17 00:00:00 2001 From: j-james Date: Fri, 2 Dec 2022 18:39:47 -0800 Subject: Day Two --- 2022/rust/day02/src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2022/rust/day02/src/main.rs (limited to '2022/rust/day02/src') diff --git a/2022/rust/day02/src/main.rs b/2022/rust/day02/src/main.rs new file mode 100644 index 0000000..fe1494f --- /dev/null +++ b/2022/rust/day02/src/main.rs @@ -0,0 +1,52 @@ +use std::env; +use std::fs; + +fn main() { + let args = env::args().nth(1).expect("missing input"); + let input = fs::read_to_string(args).unwrap(); + let moves = input.trim().split("\n").map(|x| { + let mut vec = x.split(" "); + return (vec.next().unwrap(), vec.next().unwrap()); + }).collect::>(); + + let mut sum = 0; + // note: without the iter "moves" is implicitly moved into the loop + for i in moves.iter() { + match i { + // (x, "X") => sum += 1, + ("A", "X") => sum += 3 + 1, + ("B", "X") => sum += 0 + 1, + ("C", "X") => sum += 6 + 1, + // (x, "Y") => sum += 2, + ("A", "Y") => sum += 6 + 2, + ("B", "Y") => sum += 3 + 2, + ("C", "Y") => sum += 0 + 2, + // (x, "Z") => sum += 3, + ("A", "Z") => sum += 0 + 3, + ("B", "Z") => sum += 6 + 3, + ("C", "Z") => sum += 3 + 3, + _ => () + } + } + println!("{}", sum); + + sum = 0; + for i in moves.iter() { + match i { + // (x, "X") => sum += 1, + ("A", "X") => sum += 3 + 0, + ("B", "X") => sum += 1 + 0, + ("C", "X") => sum += 2 + 0, + // (x, "Y") => sum += 2, + ("A", "Y") => sum += 1 + 3, + ("B", "Y") => sum += 2 + 3, + ("C", "Y") => sum += 3 + 3, + // (x, "Z") => sum += 3, + ("A", "Z") => sum += 2 + 6, + ("B", "Z") => sum += 3 + 6, + ("C", "Z") => sum += 1 + 6, + _ => () + } + } + println!("{}", sum); +} -- cgit v1.2.3-70-g09d2