From c8d32ae16184f660b441a3ddc490a1dae44a2eb1 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 2 Dec 2022 20:35:11 +0100 Subject: Rust nasty abstracted strategies --- AoC2022/02/solver.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'AoC2022/02') diff --git a/AoC2022/02/solver.rs b/AoC2022/02/solver.rs index 2fd1e62..dc47354 100644 --- a/AoC2022/02/solver.rs +++ b/AoC2022/02/solver.rs @@ -96,9 +96,47 @@ fn solver(strategy: &dyn Fn(&str, Hand) -> Hand) -> u32 { } }) } +// Super simple strategies +fn fixed_plays2(line: &str) -> u32 { + match line { + "A X" => 3 + 1, // draw rock + "A Y" => 6 + 2, // win paper + "A Z" => 0 + 3, // loose scissors + "B X" => 0 + 1, // loose rock + "B Y" => 3 + 2, // draw paper + "B Z" => 6 + 3, // win scissors + "C X" => 6 + 1, // win rock + "C Y" => 0 + 2, // loose paper + "C Z" => 3 + 3, // draw scissors + &_ => panic!("Invalid play"), + } +} +fn reactive_plays2(line: &str) -> u32 { + match line { + "A X" => 0 + 3, // loose scissors + "A Y" => 3 + 1, // draw paper + "A Z" => 6 + 2, // win + "B X" => 0 + 1, // loose + "B Y" => 3 + 2, // draw + "B Z" => 6 + 3, // win + "C X" => 0 + 2, // loose + "C Y" => 3 + 3, // draw + "C Z" => 6 + 1, // win + &_ => panic!("Invalid play"), + } +} +fn solver2(strategy: &dyn Fn(&str) -> u32) -> u32 { + let file = fs::File::open("input").unwrap(); + io::BufReader::new(file) + .lines() + .map(|x| strategy(&x.unwrap())) + .sum() +} fn main() { assert_eq!(12535, solver(&fixed_plays)); assert_eq!(15457, solver(&reactive_plays)); + assert_eq!(12535, solver2(&fixed_plays2)); + assert_eq!(15457, solver2(&reactive_plays2)); println!("All test passed.") } -- cgit v1.2.3