Add benchmark, clean up code
This commit is contained in:
Родитель
13b38b6085
Коммит
c97e04d0a1
|
@ -10,3 +10,7 @@ aoc-lib = { path = "../aoc-lib" }
|
|||
|
||||
[dev-dependencies]
|
||||
criterion = "0.3"
|
||||
|
||||
[[bench]]
|
||||
name = "bench"
|
||||
harness = false
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
// uncomment for unstable version
|
||||
// #![allow(unused_attributes, incomplete_features)]
|
||||
// #![feature(generic_const_exprs, const_for, const_mut_refs)]
|
||||
|
||||
use aoc_lib::AdventOfCode;
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
|
||||
#[path = "../src/main.rs"]
|
||||
mod main;
|
||||
|
||||
fn bench_main(c: &mut Criterion) {
|
||||
c.bench_function("parse input", |b| {
|
||||
let input = include_str!("../input.txt");
|
||||
b.iter(|| main::Day7::parse_input(black_box(input)))
|
||||
});
|
||||
|
||||
c.bench_function("solve 1", |b| {
|
||||
let input = main::Day7::parse_input(include_str!("../input.txt"));
|
||||
b.iter(|| main::Day7::solve_1(black_box(&input)))
|
||||
});
|
||||
|
||||
c.bench_function("solve 2", |b| {
|
||||
let input = main::Day7::parse_input(include_str!("../input.txt"));
|
||||
b.iter(|| main::Day7::solve_2(black_box(&input)))
|
||||
});
|
||||
|
||||
c.bench_function("parse sample input", |b| {
|
||||
let input = include_str!("../sample.txt");
|
||||
b.iter(|| main::Day7::parse_input(black_box(input)))
|
||||
});
|
||||
|
||||
c.bench_function("solve 1 (sample input)", |b| {
|
||||
let input = main::Day7::parse_input(include_str!("../sample.txt"));
|
||||
b.iter(|| main::Day7::solve_1(black_box(&input)))
|
||||
});
|
||||
|
||||
c.bench_function("solve 2 (sample input)", |b| {
|
||||
let input = main::Day7::parse_input(include_str!("../sample.txt"));
|
||||
b.iter(|| main::Day7::solve_2(black_box(&input)))
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_main);
|
||||
criterion_main!(benches);
|
|
@ -17,16 +17,14 @@ impl AdventOfCode for Day7 {
|
|||
let min = *input.iter().min().unwrap();
|
||||
let max = *input.iter().max().unwrap();
|
||||
|
||||
let mut lowest_cost = usize::MAX;
|
||||
for i in min..=max {
|
||||
let cost = input
|
||||
(min..=max).fold(usize::MAX, |acc, i| {
|
||||
acc.min(
|
||||
input
|
||||
.iter()
|
||||
.map(|&x| (x as isize - i as isize).abs() as usize)
|
||||
.sum();
|
||||
lowest_cost = lowest_cost.min(cost);
|
||||
}
|
||||
|
||||
lowest_cost
|
||||
.map(|&x| ((x as isize - i as isize).abs() as usize))
|
||||
.sum(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn solve_2(input: &Self::Input) -> Self::Output {
|
||||
|
@ -34,19 +32,19 @@ impl AdventOfCode for Day7 {
|
|||
let min = *input.iter().min().unwrap();
|
||||
let max = *input.iter().max().unwrap();
|
||||
|
||||
let mut lowest_cost = usize::MAX;
|
||||
for i in min..=max {
|
||||
let cost = input
|
||||
(min..=max).fold(usize::MAX, |acc, i| {
|
||||
acc.min(
|
||||
input
|
||||
.iter()
|
||||
.map(|&x| calculate_fuel((x as isize - i as isize).abs() as usize))
|
||||
.sum();
|
||||
lowest_cost = lowest_cost.min(cost);
|
||||
}
|
||||
|
||||
lowest_cost
|
||||
.sum(),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn calculate_fuel(input: usize) -> usize {
|
||||
// seems like rust is very smart: https://godbolt.org/z/WscWfvfdr
|
||||
// TODO: figure out this algorithm and implement it myself
|
||||
(1..=input).sum()
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче