From e94156f204d594615a298697b61f7665485bb1cd Mon Sep 17 00:00:00 2001 From: mat ess Date: Fri, 2 Dec 2022 23:59:59 -0500 Subject: [PATCH] Slightly reduce boilerplate --- src/main.rs | 127 +++++++++++++++++++++++++++------------------------- 1 file changed, 66 insertions(+), 61 deletions(-) diff --git a/src/main.rs b/src/main.rs index af5a404..9084826 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,6 @@ use anyhow::Error; use chrono::{Datelike, Local}; use pico_args::Arguments; -const DAYS: usize = 2; -type Part = fn(String) -> u64; -type Day = [Part; 2]; -const SOLNS: [Day; DAYS] = [[day_1_part_1, day_1_part_2], [day_2_part_1, day_2_part_2]]; - fn main() -> Result<(), Error> { let mut args = Arguments::from_env(); let small = args.contains("--small"); @@ -25,39 +20,72 @@ fn main() -> Result<(), Error> { Ok(()) } -fn day_1_part_1(input: String) -> u64 { - let lines = input.lines(); - let (max, _) = lines.fold((0, 0), |(max, sum), line| { - if line.is_empty() { - (max.max(sum), 0) - } else { - let cal = line.parse::().expect("Got bad line"); - (max, sum + cal) - } - }); - max -} - -fn day_1_part_2(input: String) -> u64 { - let lines = input.lines(); - let (a, b, c, _) = lines.fold((0, 0, 0, 0), |(a, b, c, sum), line| { - if line.trim().is_empty() { - if sum > a { - (sum, a, b, 0) - } else if sum > b { - (a, sum, b, 0) - } else if sum > c { - (a, b, sum, 0) - } else { - (a, b, c, 0) - } - } else { - let cal = line.trim().parse::().expect("Got bad line"); - (a, b, c, sum + cal) - } - }); - a + b + c -} +type Part = fn(String) -> u64; +type Day = [Part; 2]; +const DAYS: usize = 2; +const SOLNS: [Day; DAYS] = [ + [ + // day 1 part 1 + |input| { + let lines = input.lines(); + let (max, _) = lines.fold((0, 0), |(max, sum), line| { + if line.is_empty() { + (max.max(sum), 0) + } else { + let cal = line.parse::().expect("Got bad line"); + (max, sum + cal) + } + }); + max + }, + // day 1 part 2 + |input| { + let lines = input.lines(); + let (a, b, c, _) = lines.fold((0, 0, 0, 0), |(a, b, c, sum), line| { + if line.trim().is_empty() { + if sum > a { + (sum, a, b, 0) + } else if sum > b { + (a, sum, b, 0) + } else if sum > c { + (a, b, sum, 0) + } else { + (a, b, c, 0) + } + } else { + let cal = line.trim().parse::().expect("Got bad line"); + (a, b, c, sum + cal) + } + }); + a + b + c + }, + ], + [ + // day 2 part 1 + |input| { + input + .lines() + .map(|line| { + let (you, me) = line.split_once(' ').expect("oof"); + let (you, me) = (RPS::from_str(you), RPS::from_str(me)); + RPS::score(you, me) + }) + .sum() + }, + // day 2 part 2 + |input| { + input + .lines() + .map(|line| { + let (you, out) = line.split_once(' ').expect("oof"); + let (you, out) = (RPS::from_str(you), Outcome::from_str(out)); + let me = out.requires(you); + RPS::score(you, me) + }) + .sum() + }, + ], +]; #[derive(Copy, Clone, PartialEq, Eq)] enum RPS { @@ -117,17 +145,6 @@ impl RPS { } } -fn day_2_part_1(input: String) -> u64 { - input - .lines() - .map(|line| { - let (you, me) = line.split_once(' ').expect("oof"); - let (you, me) = (RPS::from_str(you), RPS::from_str(me)); - RPS::score(you, me) - }) - .sum() -} - #[derive(Copy, Clone)] enum Outcome { Win, @@ -153,15 +170,3 @@ impl Outcome { } } } - -fn day_2_part_2(input: String) -> u64 { - input - .lines() - .map(|line| { - let (you, out) = line.split_once(' ').expect("oof"); - let (you, out) = (RPS::from_str(you), Outcome::from_str(out)); - let me = out.requires(you); - RPS::score(you, me) - }) - .sum() -}