aoc2022/two.rs

65 lines
1.3 KiB
Rust
Raw Normal View History

2022-12-02 09:12:07 -06:00
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];
2022-12-03 11:51:33 -06:00
let result = chars[2];
2022-12-02 09:12:07 -06:00
2022-12-03 11:51:33 -06:00
let choice = match result {
'X' => will_lose(opponent),
'Y' => will_draw(opponent),
'Z' => will_win(opponent),
_ => continue,
2022-12-02 09:12:07 -06:00
};
2022-12-03 11:51:33 -06:00
total += match result {
'Y' => SCORE_DRAW,
'Z' => SCORE_WIN,
_ => 0,
};
2022-12-02 09:12:07 -06:00
2022-12-03 11:51:33 -06:00
total += match choice {
'A' => SCORE_ROCK,
'B' => SCORE_PAPER,
'C' => SCORE_SCISSORS,
_ => 0,
};
2022-12-02 09:12:07 -06:00
}
println!("{}", total);
}
2022-12-03 11:51:33 -06:00
fn will_win(opponent: char) -> char {
match opponent {
'A' => 'B',
'B' => 'C',
'C' => 'A',
_ => 'X',
}
2022-12-02 09:12:07 -06:00
}
2022-12-03 11:51:33 -06:00
fn will_draw(opponent: char) -> char {
opponent
}
fn will_lose(opponent: char) -> char {
match opponent {
'A' => 'C',
'B' => 'A',
'C' => 'B',
_ => 'X',
}
2022-12-02 09:12:07 -06:00
}