Slightly reduce boilerplate
parent
6891308384
commit
e94156f204
71
src/main.rs
71
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,7 +20,13 @@ fn main() -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn day_1_part_1(input: String) -> u64 {
|
||||
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() {
|
||||
|
@ -36,9 +37,9 @@ fn day_1_part_1(input: String) -> u64 {
|
|||
}
|
||||
});
|
||||
max
|
||||
}
|
||||
|
||||
fn day_1_part_2(input: String) -> u64 {
|
||||
},
|
||||
// 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() {
|
||||
|
@ -57,7 +58,34 @@ fn day_1_part_2(input: String) -> u64 {
|
|||
}
|
||||
});
|
||||
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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue