generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
68 lines (58 loc) · 1.94 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::{env, fs};
pub mod aoc_cli;
pub mod commands;
pub mod runner;
pub use day::*;
mod day;
mod readme_benchmarks;
mod run_multi;
mod timings;
pub const ANSI_ITALIC: &str = "\x1b[3m";
pub const ANSI_BOLD: &str = "\x1b[1m";
pub const ANSI_RESET: &str = "\x1b[0m";
/// Helper function that reads a text file to a string.
#[must_use]
pub fn read_file(folder: &str, day: Day) -> String {
let cwd = env::current_dir().unwrap();
let filepath = cwd.join("data").join(folder).join(format!("{day}.txt"));
let f = fs::read_to_string(filepath);
f.expect("could not open input file")
}
/// Helper function that reads a text file to string, appending a part suffix. E.g. like `01-2.txt`.
#[must_use]
pub fn read_file_part(folder: &str, day: Day, part: u8) -> String {
let cwd = env::current_dir().unwrap();
let filepath = cwd
.join("data")
.join(folder)
.join(format!("{day}-{part}.txt"));
let f = fs::read_to_string(filepath);
f.expect("could not open input file")
}
/// Creates the constant `DAY` and sets up the input and runner for each part.
///
/// The optional, second parameter (1 or 2) allows you to only run a single part of the solution.
#[macro_export]
macro_rules! solution {
($day:expr) => {
$crate::solution!(@impl $day, [part_one, 1] [part_two, 2]);
};
($day:expr, 1) => {
$crate::solution!(@impl $day, [part_one, 1]);
};
($day:expr, 2) => {
$crate::solution!(@impl $day, [part_two, 2]);
};
(@impl $day:expr, $( [$func:expr, $part:expr] )*) => {
/// The current day.
const DAY: $crate::template::Day = $crate::day!($day);
#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
fn main() {
use $crate::template::runner::*;
let input = $crate::template::read_file("inputs", DAY);
$( run_part($func, &input, DAY, $part); )*
}
};
}