aoc_utils/
lib.rs

1//! The minimal set of utils for writing [Advent of Code](https://adventofcode.com/) solutions.
2
3mod args;
4mod bufwrap;
5
6pub use args::AocCommand;
7pub use bufwrap::BufferedInput;
8
9use std::fmt::Display;
10use std::time::Instant;
11
12/// Runs the code, and prints out the elapsed time and the result.
13///
14/// First prints the elapsed time to stderr,
15/// then the result to stdout, in separate lines.
16///
17/// # Example
18///
19/// ```
20/// use aoc_utils::measure_and_print;
21///
22/// measure_and_print(|| {
23///     (0..10_000).sum::<u64>()
24/// });
25///
26/// // Prints:
27/// // 227.81 μs
28/// // 49995000
29/// ```
30pub fn measure_and_print<T: Display, F: FnOnce() -> T>(f: F) {
31    let start = Instant::now();
32    let result = f();
33    let elapsed = start.elapsed();
34
35    eprintln!("{:?}", elapsed);
36    println!("{}", result);
37}