aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/02
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/02')
-rw-r--r--AoC2022/02/solver.rs52
1 files changed, 52 insertions, 0 deletions
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::<Vec<_>>()[..] {
+ score += fight(translate(my), translate(ot)) + weight(translate(my));
+ } else {
+ todo!()
+ };
+ }
+
+ println!("{}", score);
+ Ok(())
+}