aoc2022/two.rs
2022-12-03 11:51:33 -06:00

65 lines
1.3 KiB
Rust

const SCORE_WIN: u32 = 6;
const SCORE_DRAW: u32 = 3;
const SCORE_ROCK: u32 = 1;
const SCORE_PAPER: u32 = 2;
const SCORE_SCISSORS: u32 = 3;
fn main() {
let buf = std::fs::read_to_string("two.txt").unwrap();
let mut total = 0u32;
for s in buf.split("\n") {
if s.is_empty() {
continue;
}
let chars = s.chars().collect::<Vec<_>>();
let opponent = chars[0];
let result = chars[2];
let choice = match result {
'X' => will_lose(opponent),
'Y' => will_draw(opponent),
'Z' => will_win(opponent),
_ => continue,
};
total += match result {
'Y' => SCORE_DRAW,
'Z' => SCORE_WIN,
_ => 0,
};
total += match choice {
'A' => SCORE_ROCK,
'B' => SCORE_PAPER,
'C' => SCORE_SCISSORS,
_ => 0,
};
}
println!("{}", total);
}
fn will_win(opponent: char) -> char {
match opponent {
'A' => 'B',
'B' => 'C',
'C' => 'A',
_ => 'X',
}
}
fn will_draw(opponent: char) -> char {
opponent
}
fn will_lose(opponent: char) -> char {
match opponent {
'A' => 'C',
'B' => 'A',
'C' => 'B',
_ => 'X',
}
}