AOC2022/rust/2/part2.rs
2022-12-02 21:49:21 +00:00

37 lines
878 B
Rust

use std::{
fs::File,
io::{BufRead, BufReader},
};
fn main() -> () {
let path = "input.txt";
let input = File::open(path).unwrap();
let buf = BufReader::new(input);
let score: i32 = buf
.lines()
.map(|line| {
let line = line.unwrap();
let opponent: i32 = match &line[0..1] {
"A" => 0,
"B" => 1,
"C" => 2,
_ => panic!("Invalid input"),
};
match &line[2..3] {
"X" => 0 + pick_score(opponent, -1),
"Y" => 3 + pick_score(opponent, 0),
"Z" => 6 + pick_score(opponent, 1),
_ => panic!("Invalid input"),
}
})
.sum();
println!("{}", score);
}
fn pick_score(opponent: i32, outcome: i32) -> i32 {
(opponent + outcome + 3) % 3 + 1
}