diff options
author | j-james | 2022-12-03 02:39:47 +0000 |
---|---|---|
committer | j-james | 2022-12-03 02:39:47 +0000 |
commit | f5f940c52b38a5e17092f5896dc2fc13e8443478 (patch) | |
tree | 433ebcfee2ea54432734b44d8f51f95fc5ac8a69 /2022/rust | |
parent | 13b3ae4211631c74b9249a6033869f75e036fad2 (diff) |
Day Two
Diffstat (limited to '2022/rust')
-rw-r--r-- | 2022/rust/day02/Cargo.lock | 7 | ||||
-rw-r--r-- | 2022/rust/day02/Cargo.toml | 8 | ||||
-rw-r--r-- | 2022/rust/day02/src/main.rs | 52 |
3 files changed, 67 insertions, 0 deletions
diff --git a/2022/rust/day02/Cargo.lock b/2022/rust/day02/Cargo.lock new file mode 100644 index 0000000..52d399b --- /dev/null +++ b/2022/rust/day02/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day02" +version = "0.1.0" diff --git a/2022/rust/day02/Cargo.toml b/2022/rust/day02/Cargo.toml new file mode 100644 index 0000000..843335d --- /dev/null +++ b/2022/rust/day02/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day02" +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/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::<Vec<(&str, &str)>>(); + + 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); +} |