feat: use cargo --bin instead of solutions crate
This commit is contained in:
36
src/lib.rs
36
src/lib.rs
@@ -1,13 +1,41 @@
|
||||
use std::env;
|
||||
use std::fs;
|
||||
|
||||
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);
|
||||
}};
|
||||
}
|
||||
|
||||
pub fn read_file(folder: &str, day: u8) -> String {
|
||||
let cwd = env::current_dir().unwrap();
|
||||
|
||||
let filepath = cwd
|
||||
.join("src")
|
||||
.join(folder)
|
||||
.join(format!("day{:02}.txt", day));
|
||||
let filepath = cwd.join("src").join(folder).join(format!("{:02}.txt", day));
|
||||
|
||||
let f = fs::read_to_string(filepath);
|
||||
f.expect("could not open input file")
|
||||
|
||||
92
src/main.rs
92
src/main.rs
@@ -1,74 +1,26 @@
|
||||
use crate::solutions::*;
|
||||
use aoc::read_file;
|
||||
use std::env;
|
||||
use std::fmt::Display;
|
||||
use std::time::Instant;
|
||||
|
||||
mod helpers;
|
||||
mod solutions;
|
||||
|
||||
static ANSI_ITALIC: &str = "\x1b[3m";
|
||||
static ANSI_BOLD: &str = "\x1b[1m";
|
||||
static ANSI_RESET: &str = "\x1b[0m";
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
macro_rules! solve_day {
|
||||
($day:path, $input:expr) => {{
|
||||
use $day::*;
|
||||
println!("----");
|
||||
println!("");
|
||||
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);
|
||||
println!("");
|
||||
println!("----");
|
||||
}};
|
||||
}
|
||||
use aoc::{ANSI_BOLD, ANSI_RESET};
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let day: u8 = args[1].clone().parse().unwrap();
|
||||
let input = read_file("inputs", day);
|
||||
(1..=25).for_each(|day| {
|
||||
let day = format!("{:02}", day);
|
||||
|
||||
match day {
|
||||
1 => solve_day!(day01, &input),
|
||||
2 => solve_day!(day02, &input),
|
||||
3 => solve_day!(day03, &input),
|
||||
4 => solve_day!(day04, &input),
|
||||
5 => solve_day!(day05, &input),
|
||||
6 => solve_day!(day06, &input),
|
||||
7 => solve_day!(day07, &input),
|
||||
8 => solve_day!(day08, &input),
|
||||
9 => solve_day!(day09, &input),
|
||||
10 => solve_day!(day10, &input),
|
||||
11 => solve_day!(day11, &input),
|
||||
12 => solve_day!(day12, &input),
|
||||
13 => solve_day!(day13, &input),
|
||||
14 => solve_day!(day14, &input),
|
||||
15 => solve_day!(day15, &input),
|
||||
16 => solve_day!(day16, &input),
|
||||
17 => solve_day!(day17, &input),
|
||||
18 => solve_day!(day18, &input),
|
||||
19 => solve_day!(day19, &input),
|
||||
20 => solve_day!(day20, &input),
|
||||
21 => solve_day!(day21, &input),
|
||||
22 => solve_day!(day22, &input),
|
||||
23 => solve_day!(day23, &input),
|
||||
24 => solve_day!(day24, &input),
|
||||
25 => solve_day!(day25, &input),
|
||||
_ => println!("day not solved: {}", day),
|
||||
}
|
||||
let cmd = Command::new("cargo")
|
||||
.args(&["run", "--release", "--bin", &day])
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
let output = String::from_utf8(cmd.stdout).unwrap();
|
||||
println!("----------");
|
||||
println!("{}| Day {} |{}", ANSI_BOLD, day, ANSI_RESET);
|
||||
println!("----------");
|
||||
println!(
|
||||
"{}",
|
||||
if !output.is_empty() {
|
||||
output
|
||||
} else {
|
||||
"Not solved.".to_string()
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user