feat: move template code into its own module
This commit is contained in:
parent
00000410c9
commit
00000420aa
|
@ -19,7 +19,7 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
|||
|
||||
[[package]]
|
||||
name = "aoc"
|
||||
version = "41.0.0"
|
||||
version = "42.0.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
@ -105,7 +105,7 @@ checksum = "03d8c417d7a8cb362e0c37e5d815f5eb7c37f79ff93707329d5a194e42e54ca0"
|
|||
|
||||
[[package]]
|
||||
name = "download"
|
||||
version = "41.0.0"
|
||||
version = "42.0.0"
|
||||
dependencies = [
|
||||
"dotenvy",
|
||||
"pico-args",
|
||||
|
@ -643,7 +643,7 @@ checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
|
|||
|
||||
[[package]]
|
||||
name = "scaffold"
|
||||
version = "41.0.0"
|
||||
version = "42.0.0"
|
||||
dependencies = [
|
||||
"dotenvy",
|
||||
"pico-args",
|
||||
|
|
|
@ -19,7 +19,7 @@ members = ["utils/download", "utils/scaffold"]
|
|||
[workspace.package]
|
||||
description = "template for advent of code"
|
||||
readme = "README.md"
|
||||
version = "41.0.0"
|
||||
version = "42.0.0"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
authors = ["Matej Janežič <janezic.mj@gmail.com>"]
|
||||
|
|
53
src/lib.rs
53
src/lib.rs
|
@ -1,53 +1,2 @@
|
|||
/*
|
||||
* This file contains template code.
|
||||
* There is no need to edit this file unless you want to change template functionality.
|
||||
* Prefer `./helpers.rs` if you want to extract code from your solutions.
|
||||
*/
|
||||
use std::env;
|
||||
use std::fs;
|
||||
pub mod template;
|
||||
|
||||
pub const ANSI_ITALIC: &str = "\x1b[3m";
|
||||
pub const ANSI_BOLD: &str = "\x1b[1m";
|
||||
pub const ANSI_RESET: &str = "\x1b[0m";
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! solution {
|
||||
($day:expr) => {
|
||||
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) -> Option<T>, input: &str) {
|
||||
let timer = Instant::now();
|
||||
let result = func(input);
|
||||
let elapsed = timer.elapsed();
|
||||
match result {
|
||||
Some(result) => {
|
||||
println!(
|
||||
"{} {}(elapsed: {:.2?}){}",
|
||||
result, ANSI_ITALIC, elapsed, ANSI_RESET
|
||||
);
|
||||
}
|
||||
None => {
|
||||
println!("not solved.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = aoc::read_file("inputs", 1);
|
||||
print!("{}Part {}{}: ", ANSI_BOLD, 1, ANSI_RESET);
|
||||
print_result(part_one, &input);
|
||||
print!("{}Part {}{}: ", ANSI_BOLD, 2, ANSI_RESET);
|
||||
print_result(part_two, &input);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn read_file(folder: &str, day: u8) -> String {
|
||||
let cwd = env::current_dir().unwrap();
|
||||
let filepath = cwd.join("data").join(folder).join(format!("{day:02}.txt"));
|
||||
let f = fs::read_to_string(filepath);
|
||||
f.expect("could not open input file").trim().to_string()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* This file contains template code.
|
||||
* There is no need to edit this file unless you want to change template functionality.
|
||||
*/
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
|
||||
pub const ANSI_ITALIC: &str = "\x1b[3m";
|
||||
pub const ANSI_BOLD: &str = "\x1b[1m";
|
||||
pub const ANSI_RESET: &str = "\x1b[0m";
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! solution {
|
||||
($day:expr) => {
|
||||
use aoc::template::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
|
||||
use std::fmt::Display;
|
||||
use std::time::Instant;
|
||||
|
||||
fn print_result<T: Display>(func: impl FnOnce(&str) -> Option<T>, input: &str) {
|
||||
let timer = Instant::now();
|
||||
let result = func(input);
|
||||
let elapsed = timer.elapsed();
|
||||
match result {
|
||||
Some(result) => {
|
||||
println!(
|
||||
"{} {}(elapsed: {:.2?}){}",
|
||||
result, ANSI_ITALIC, elapsed, ANSI_RESET
|
||||
);
|
||||
}
|
||||
None => {
|
||||
println!("not solved.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = aoc::template::read_file("inputs", 1);
|
||||
print!("{}Part {}{}: ", ANSI_BOLD, 1, ANSI_RESET);
|
||||
print_result(part_one, &input);
|
||||
print!("{}Part {}{}: ", ANSI_BOLD, 2, ANSI_RESET);
|
||||
print_result(part_two, &input);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn read_file(folder: &str, day: u8) -> String {
|
||||
let cwd = env::current_dir().unwrap();
|
||||
let filepath = cwd.join("data").join(folder).join(format!("{day:02}.txt"));
|
||||
let f = fs::read_to_string(filepath);
|
||||
f.expect("could not open input file").trim().to_string()
|
||||
}
|
|
@ -23,11 +23,11 @@ mod tests {
|
|||
use super::*;
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
assert_eq!(part_one(&aoc::read_file("examples", DAY)), None);
|
||||
assert_eq!(part_one(&aoc::template::read_file("examples", DAY)), None);
|
||||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(part_two(&aoc::read_file("examples", DAY)), None);
|
||||
assert_eq!(part_two(&aoc::template::read_file("examples", DAY)), None);
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
|
Loading…
Reference in New Issue