Skip to content

Instantly share code, notes, and snippets.

@saolsen
Created December 2, 2022 13:40
Show Gist options
  • Save saolsen/a01df056a2086032a74629410552f31a to your computer and use it in GitHub Desktop.
Save saolsen/a01df056a2086032a74629410552f31a to your computer and use it in GitHub Desktop.
aoc 2022 day02
#[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