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 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::<u64>().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::<u64>().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::<u64>().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::<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)]
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()
}