[CLEANUP] re-ordered some of the code
This commit is contained in:
116
src/main.rs
116
src/main.rs
@@ -8,67 +8,9 @@ mod day1;
|
|||||||
mod day_solver;
|
mod day_solver;
|
||||||
mod day2;
|
mod day2;
|
||||||
|
|
||||||
|
|
||||||
const MAX_DAY: u8 = 2;
|
const MAX_DAY: u8 = 2;
|
||||||
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
||||||
|
|
||||||
fn build_day_solver(day: u8) -> Option<Box<dyn DaySolver>> {
|
|
||||||
match day {
|
|
||||||
1 => Some(Box::new(Day1::create())),
|
|
||||||
2 => Some(Box::new(Day2::create())),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bench<F, K>(mut f: F) -> (K, u128)
|
|
||||||
where F: FnMut() -> K {
|
|
||||||
let now = Instant::now();
|
|
||||||
let res = f();
|
|
||||||
(res, now.elapsed().as_micros())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn solve(day: u8, silent: bool) -> AocBenchResult {
|
|
||||||
|
|
||||||
let now = Instant::now();
|
|
||||||
let (solver, init_time) = bench(|| build_day_solver(day));
|
|
||||||
|
|
||||||
let part1_time: u128;
|
|
||||||
let part2_time: u128;
|
|
||||||
match solver {
|
|
||||||
Some(mut s) => {
|
|
||||||
|
|
||||||
let(part1, pt1_time) = bench(|| s.solve_part1());
|
|
||||||
part1_time = pt1_time;
|
|
||||||
|
|
||||||
if !silent {
|
|
||||||
println!("Day {} Part 1: {}", day, part1);
|
|
||||||
}
|
|
||||||
let (part2, pt2_time) = bench(|| s.solve_part2());
|
|
||||||
part2_time = pt2_time;
|
|
||||||
if !silent {
|
|
||||||
println!("Day {} Part 1: {}", day, part2);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => panic!("This day is not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
return AocBenchResult{
|
|
||||||
init: init_time,
|
|
||||||
part1: part1_time,
|
|
||||||
part2: part2_time,
|
|
||||||
total: now.elapsed().as_micros()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
struct AocBenchResult {
|
|
||||||
init: u128,
|
|
||||||
part1: u128,
|
|
||||||
part2: u128,
|
|
||||||
total: u128
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
@@ -120,6 +62,57 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
struct AocBenchResult {
|
||||||
|
init: u128,
|
||||||
|
part1: u128,
|
||||||
|
part2: u128,
|
||||||
|
total: u128
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_day_solver(day: u8) -> Option<Box<dyn DaySolver>> {
|
||||||
|
match day {
|
||||||
|
1 => Some(Box::new(Day1::create())),
|
||||||
|
2 => Some(Box::new(Day2::create())),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve(day: u8, silent: bool) -> AocBenchResult {
|
||||||
|
|
||||||
|
let now = Instant::now();
|
||||||
|
let (solver, init_time) = bench(|| build_day_solver(day));
|
||||||
|
|
||||||
|
let part1_time: u128;
|
||||||
|
let part2_time: u128;
|
||||||
|
match solver {
|
||||||
|
Some(mut s) => {
|
||||||
|
|
||||||
|
let(part1, pt1_time) = bench(|| s.solve_part1());
|
||||||
|
part1_time = pt1_time;
|
||||||
|
|
||||||
|
if !silent {
|
||||||
|
println!("Day {} Part 1: {}", day, part1);
|
||||||
|
}
|
||||||
|
let (part2, pt2_time) = bench(|| s.solve_part2());
|
||||||
|
part2_time = pt2_time;
|
||||||
|
if !silent {
|
||||||
|
println!("Day {} Part 1: {}", day, part2);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => panic!("This day is not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
return AocBenchResult{
|
||||||
|
init: init_time,
|
||||||
|
part1: part1_time,
|
||||||
|
part2: part2_time,
|
||||||
|
total: now.elapsed().as_micros()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn print_bench_result<F>(bench_results: &Vec<AocBenchResult>, f: F, bench_part_description: &str)
|
fn print_bench_result<F>(bench_results: &Vec<AocBenchResult>, f: F, bench_part_description: &str)
|
||||||
where
|
where
|
||||||
F: FnMut(&AocBenchResult) -> u128 {
|
F: FnMut(&AocBenchResult) -> u128 {
|
||||||
@@ -155,4 +148,11 @@ fn solve_all(silent: bool) -> AocBenchResult {
|
|||||||
total: bench_results.iter().map(|t| t.total).sum(),
|
total: bench_results.iter().map(|t| t.total).sum(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bench<F, K>(mut f: F) -> (K, u128)
|
||||||
|
where F: FnMut() -> K {
|
||||||
|
let now = Instant::now();
|
||||||
|
let res = f();
|
||||||
|
(res, now.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user