Created
December 2, 2022 13:40
-
-
Save saolsen/a01df056a2086032a74629410552f31a to your computer and use it in GitHub Desktop.
aoc 2022 day02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Clone, Copy)] | |
enum Move { | |
Rock, | |
Paper, | |
Scissors | |
} | |
impl Move { | |
pub fn from(s: &str) -> Move{ | |
use Move::*; | |
match s { | |
"A" | "X" => Rock, | |
"B" | "Y" => Paper, | |
"C" | "Z" => Scissors, | |
_ => unreachable!() | |
} | |
} | |
} | |
#[derive(Clone, Copy)] | |
enum End { | |
Lose, | |
Draw, | |
Win | |
} | |
impl End { | |
pub fn from(s: &str) -> End{ | |
use End::*; | |
match s { | |
"X" => Lose, | |
"Y" => Draw, | |
"Z" => Win, | |
_ => unreachable!() | |
} | |
} | |
} | |
// part 2 | |
fn main() { | |
use Move::*; | |
use End::*; | |
let input = include_str!("day02_input.txt"); | |
let mut total_score = 0; | |
for line in input.split('\n') { | |
let mut split = line.split(' '); | |
let (them, end) = (Move::from(split.next().unwrap()), End::from(split.next().unwrap())); | |
let mut score = 0; | |
let me = match (them, end) { | |
(_, Draw) => them, | |
(Rock, Lose) | (Paper, Win) => Scissors, | |
(Rock, Win) | (Scissors, Lose)=> Paper, | |
(Paper, Lose) | (Scissors, Win) => Rock, | |
}; | |
match me { | |
Rock => score += 1, | |
Paper => score += 2, | |
Scissors => score += 3, | |
}; | |
match end { | |
Lose => score += 0, | |
Draw => score += 3, | |
Win => score += 6, | |
}; | |
total_score += score; | |
} | |
eprintln!("total score {total_score}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment