aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-02 19:32:49 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-02 19:32:49 +0100
commitcd25edb1ded40bd5bf328b928dc533c53cecaa05 (patch)
treeb76fdba30688a90d9021f1bb7390192a6c90c378 /AoC2022
parent08e588c7e3ec7606ddd66a968c6aa586b54edf69 (diff)
downloadscratch-cd25edb1ded40bd5bf328b928dc533c53cecaa05.tar.gz
scratch-cd25edb1ded40bd5bf328b928dc533c53cecaa05.tar.bz2
scratch-cd25edb1ded40bd5bf328b928dc533c53cecaa05.zip
[AoC2022] Rust 02-01
Diffstat (limited to 'AoC2022')
-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(())
+}