allow passing an argument to part/bench functions

This commit is contained in:
kageru 2022-12-15 10:53:56 +01:00
parent a4ad70ddd3
commit 3926f7fcd7
3 changed files with 61 additions and 7 deletions

25
2022/inputs/day15 Normal file
View File

@ -0,0 +1,25 @@
Sensor at x=3729579, y=1453415: closest beacon is at x=4078883, y=2522671
Sensor at x=3662668, y=2749205: closest beacon is at x=4078883, y=2522671
Sensor at x=257356, y=175834: closest beacon is at x=1207332, y=429175
Sensor at x=2502777, y=3970934: closest beacon is at x=3102959, y=3443573
Sensor at x=24076, y=2510696: closest beacon is at x=274522, y=2000000
Sensor at x=3163363, y=3448163: closest beacon is at x=3102959, y=3443573
Sensor at x=1011369, y=447686: closest beacon is at x=1207332, y=429175
Sensor at x=3954188, y=3117617: closest beacon is at x=4078883, y=2522671
Sensor at x=3480746, y=3150039: closest beacon is at x=3301559, y=3383795
Sensor at x=2999116, y=3137910: closest beacon is at x=3102959, y=3443573
Sensor at x=3546198, y=462510: closest beacon is at x=3283798, y=-405749
Sensor at x=650838, y=1255586: closest beacon is at x=274522, y=2000000
Sensor at x=3231242, y=3342921: closest beacon is at x=3301559, y=3383795
Sensor at x=1337998, y=31701: closest beacon is at x=1207332, y=429175
Sensor at x=1184009, y=3259703: closest beacon is at x=2677313, y=2951659
Sensor at x=212559, y=1737114: closest beacon is at x=274522, y=2000000
Sensor at x=161020, y=2251470: closest beacon is at x=274522, y=2000000
Sensor at x=3744187, y=3722432: closest beacon is at x=3301559, y=3383795
Sensor at x=2318112, y=2254019: closest beacon is at x=2677313, y=2951659
Sensor at x=2554810, y=56579: closest beacon is at x=3283798, y=-405749
Sensor at x=1240184, y=897870: closest beacon is at x=1207332, y=429175
Sensor at x=2971747, y=2662873: closest beacon is at x=2677313, y=2951659
Sensor at x=3213584, y=3463821: closest beacon is at x=3102959, y=3443573
Sensor at x=37652, y=3969055: closest beacon is at x=-615866, y=3091738
Sensor at x=1804153, y=1170987: closest beacon is at x=1207332, y=429175

29
2022/src/bin/day15.rs Normal file
View File

@ -0,0 +1,29 @@
#![feature(test)]
extern crate test;
use aoc2022::{boilerplate, common::*};
const DAY: usize = 15;
type Parsed = Vec<usize>;
fn parse_input(raw: &str) -> Parsed {
parse_nums(raw)
}
fn part1(parsed: &Parsed, row: i64) -> usize {
unimplemented!()
}
fn part2(parsed: &Parsed) -> usize {
unimplemented!()
}
boilerplate! {
TEST_INPUT == "",
tests: {
part1: { TEST_INPUT, 10 => 0 },
part2: { TEST_INPUT => 0 },
},
bench1(2_000_000) == 0,
bench2 == 0,
bench_parse: Vec::len => 0,
}

View File

@ -3,19 +3,19 @@ macro_rules! boilerplate {
(
TEST_INPUT == $ti: literal,
tests: {
$($part: ident: { $($tpi: expr => $to: expr),+$(,)? }),*$(,)?
$($part: ident: { $($tpi: expr $(,$ati: expr)* => $to: expr),+$(,)? }),*$(,)?
},
$(unittests: {
$($unittest: ident: { $($($utpi: expr),+ => $uto: expr),+$(,)? }),*$(,)?
},)?
bench1 == $b1: literal,
bench1$(($bi1: literal))? == $b1: literal,
bench2 == $b2: literal,
bench_parse: $input_fn: expr => $it: expr$(,)?
) => {
fn main() {
let raw_input = read_file(DAY);
let input = parse_input(&raw_input);
println!("Part 1: {}", part1(&input));
println!("Part 1: {}", part1(&input$(,$bi1)?));
println!("Part 2: {}", part2(&input));
}
@ -36,10 +36,10 @@ macro_rules! boilerplate {
#[test]
fn [<$part _test_ $to:lower>]() {
let input = parse_input($tpi);
assert_eq!($part(&input), $to);
assert_eq!($part(&input, $($ati),*), $to);
}
})+)*
bench!(part1() == $b1);
bench!(part1($($bi1)?) == $b1);
bench!(part2() == $b2);
#[bench]
fn bench_input_parsing(b: &mut test::Bencher) {
@ -52,13 +52,13 @@ macro_rules! boilerplate {
#[macro_export]
macro_rules! bench {
($part: ident() == $expected:expr) => {
($part: ident($($bi: literal)?) == $expected:expr) => {
paste::paste! {
#[bench]
fn [<$part _bench>](b: &mut test::Bencher) {
let raw = &read_file(DAY);
let input = parse_input(&raw);
b.iter(|| assert_eq!($part(test::black_box(&input)), $expected));
b.iter(|| assert_eq!($part(test::black_box(&input)$(, $bi)?), $expected));
}
}
};