diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-02 20:35:11 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-02 20:39:53 +0100 |
commit | c8d32ae16184f660b441a3ddc490a1dae44a2eb1 (patch) | |
tree | 5a9a5123631596696336392384ee5359e21456f9 /AoC2022/02 | |
parent | b5c8aae7a0497810331759b47a22f0b2ebe765b1 (diff) | |
download | scratch-c8d32ae16184f660b441a3ddc490a1dae44a2eb1.tar.gz scratch-c8d32ae16184f660b441a3ddc490a1dae44a2eb1.tar.bz2 scratch-c8d32ae16184f660b441a3ddc490a1dae44a2eb1.zip |
Rust nasty abstracted strategies
Diffstat (limited to 'AoC2022/02')
-rw-r--r-- | AoC2022/02/solver.rs | 38 |
1 files changed, 38 insertions, 0 deletions
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.") } |