diff --git a/day02/Cargo.lock b/day02/Cargo.lock new file mode 100644 index 0000000..52d399b --- /dev/null +++ b/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/day02/Cargo.toml b/day02/Cargo.toml new file mode 100644 index 0000000..843335d --- /dev/null +++ b/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/day02/src/main.rs b/day02/src/main.rs new file mode 100644 index 0000000..93796a8 --- /dev/null +++ b/day02/src/main.rs @@ -0,0 +1,69 @@ +use std::error; +use std::fs::File; +use std::io::BufRead; +use std::io::BufReader; + +#[derive(PartialEq)] +enum Values { + Rock, + Paper, + Scissors, +} + + +impl Values { + fn get_from_str(v: &str) -> Values { + match v { + "A" => Values::Rock, + "B" => Values::Paper, + "C" => Values::Scissors, + "X" => Values::Rock, + "Y" => Values::Paper, + "Z" => Values::Scissors, + _ => panic!("Unexpected value for Values") + } + } + + fn get_u32(&self) -> u32 { + match self { + Values::Rock => 1, + Values::Paper => 2, + Values::Scissors => 3 + } + } + + fn beats(self, other: Values) -> bool { + if self == Values::Rock { + other == Values::Scissors + } else if self == Values::Paper { + other == Values::Rock + } else if self == Values::Scissors { + other == Values::Paper + } else { + false + } + } +} + + +fn main() -> Result<(), Box> { + let f = File::open("input.txt")?; + let reader = BufReader::new(f); + + let mut sum: u32 = 0; + for line in reader.lines() { + let line = line?; + let split_result: Vec<&str> = line.split_whitespace().collect(); + let other = Values::get_from_str(&split_result[0]); + let my = Values::get_from_str(&split_result[1]); + sum += my.get_u32(); + if other == my { + sum += 3; + } else if my.beats(other) { + sum += 6; + } + } + + println!("{}", sum); + Ok(()) +}