Slightly reduce boilerplate

main
mat ess 2022-12-02 23:59:59 -05:00
parent 6891308384
commit e94156f204
1 changed files with 66 additions and 61 deletions

View File

@ -5,11 +5,6 @@ use anyhow::Error;
use chrono::{Datelike, Local}; use chrono::{Datelike, Local};
use pico_args::Arguments; 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> { fn main() -> Result<(), Error> {
let mut args = Arguments::from_env(); let mut args = Arguments::from_env();
let small = args.contains("--small"); let small = args.contains("--small");
@ -25,39 +20,72 @@ fn main() -> Result<(), Error> {
Ok(()) Ok(())
} }
fn day_1_part_1(input: String) -> u64 { type Part = fn(String) -> u64;
let lines = input.lines(); type Day = [Part; 2];
let (max, _) = lines.fold((0, 0), |(max, sum), line| { const DAYS: usize = 2;
if line.is_empty() { const SOLNS: [Day; DAYS] = [
(max.max(sum), 0) [
} else { // day 1 part 1
let cal = line.parse::<u64>().expect("Got bad line"); |input| {
(max, sum + cal) let lines = input.lines();
} let (max, _) = lines.fold((0, 0), |(max, sum), line| {
}); if line.is_empty() {
max (max.max(sum), 0)
} } else {
let cal = line.parse::<u64>().expect("Got bad line");
fn day_1_part_2(input: String) -> u64 { (max, sum + cal)
let lines = input.lines(); }
let (a, b, c, _) = lines.fold((0, 0, 0, 0), |(a, b, c, sum), line| { });
if line.trim().is_empty() { max
if sum > a { },
(sum, a, b, 0) // day 1 part 2
} else if sum > b { |input| {
(a, sum, b, 0) let lines = input.lines();
} else if sum > c { let (a, b, c, _) = lines.fold((0, 0, 0, 0), |(a, b, c, sum), line| {
(a, b, sum, 0) if line.trim().is_empty() {
} else { if sum > a {
(a, b, c, 0) (sum, a, b, 0)
} } else if sum > b {
} else { (a, sum, b, 0)
let cal = line.trim().parse::<u64>().expect("Got bad line"); } else if sum > c {
(a, b, c, sum + cal) (a, b, sum, 0)
} } else {
}); (a, b, c, 0)
a + b + c }
} } else {
let cal = line.trim().parse::<u64>().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)] #[derive(Copy, Clone, PartialEq, Eq)]
enum RPS { 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)] #[derive(Copy, Clone)]
enum Outcome { enum Outcome {
Win, 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()
}