2021-12-29 14:12:01 +01:00
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
|
|
|
|
2021-12-29 14:36:48 +01:00
|
|
|
pub static ANSI_ITALIC: &str = "\x1b[3m";
|
|
|
|
pub static ANSI_BOLD: &str = "\x1b[1m";
|
|
|
|
pub static ANSI_RESET: &str = "\x1b[0m";
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! solve_day {
|
|
|
|
($input:expr, $part_one:ident, $part_two:ident) => {{
|
|
|
|
use aoc::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
|
|
|
|
use std::fmt::Display;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
fn print_result<T: Display>(func: impl FnOnce(&str) -> T, input: &str) {
|
|
|
|
let timer = Instant::now();
|
|
|
|
let result = func(input);
|
|
|
|
let time = timer.elapsed();
|
|
|
|
println!(
|
|
|
|
"{} {}(elapsed: {:.2?}){}",
|
|
|
|
result, ANSI_ITALIC, time, ANSI_RESET
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("🎄 {}Part 1{} 🎄", ANSI_BOLD, ANSI_RESET);
|
|
|
|
println!("");
|
|
|
|
print_result($part_one, $input);
|
|
|
|
println!("");
|
|
|
|
println!("🎄 {}Part 2{} 🎄", ANSI_BOLD, ANSI_RESET);
|
|
|
|
println!("");
|
|
|
|
print_result($part_two, $input);
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2021-12-29 14:12:01 +01:00
|
|
|
pub fn read_file(folder: &str, day: u8) -> String {
|
|
|
|
let cwd = env::current_dir().unwrap();
|
|
|
|
|
2021-12-29 14:36:48 +01:00
|
|
|
let filepath = cwd.join("src").join(folder).join(format!("{:02}.txt", day));
|
2021-12-29 14:12:01 +01:00
|
|
|
|
|
|
|
let f = fs::read_to_string(filepath);
|
|
|
|
f.expect("could not open input file")
|
|
|
|
}
|