AoC2022/src/bin/day2.rs

79 lines
1.8 KiB
Rust

use std::error::Error;
use std::fs::File;
use std::io::{self, BufRead};
use std::vec::Vec;
fn calc_points(other: &str, me: &str) -> u32 {
match other {
"A" => match me {
"X" => 1 + 3,
"Y" => 2 + 6,
"Z" => 3 + 0,
_ => 0,
},
"B" => match me {
"X" => 1 + 0,
"Y" => 2 + 3,
"Z" => 3 + 6,
_ => 0,
},
"C" => match me {
"X" => 1 + 6,
"Y" => 2 + 0,
"Z" => 3 + 3,
_ => 0,
},
_ => 0,
}
}
fn calc_points2(other: &str, me: &str) -> u32 {
match other {
"A" => match me {
"X" => 3 + 0,
"Y" => 1 + 3,
"Z" => 2 + 6,
_ => 0,
},
"B" => match me {
"X" => 1 + 0,
"Y" => 2 + 3,
"Z" => 3 + 6,
_ => 0,
},
"C" => match me {
"X" => 2 + 0,
"Y" => 3 + 3,
"Z" => 1 + 6,
_ => 0,
},
_ => 0,
}
}
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("inputs/day2.txt")?;
let lines = io::BufReader::new(file).lines().map(|l| l.unwrap());
let rounds: Vec<(String, String)> = lines
.map(|l| {
println!("line: {}", l);
let mut parts = l.split(" ");
(
parts.next().unwrap().to_owned(),
parts.next().unwrap().to_owned(),
)
})
.collect();
let points = rounds.iter().fold(0, |p, r| p + calc_points(&r.0, &r.1));
println!("Answer Part1: {}", points);
let points2 = rounds.iter().fold(0, |p, r| p + calc_points2(&r.0, &r.1));
println!("Answer Part2: {}", points2);
Ok(())
}