From: Greg Burri Date: Tue, 10 Dec 2024 21:05:37 +0000 (+0100) Subject: Move some functions to an external crate. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=b548c449ac6953b4b85f78d40e874d2900c0efc0;p=advent_of_code_2024.git Move some functions to an external crate. --- diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4413612 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "advent_of_code_common"] + path = advent_of_code_common + url = ssh://gitolite3@gburri.org:9851/advent_of_code_common.git diff --git a/Cargo.toml b/Cargo.toml index 7175c05..18a05b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,10 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +advent_of_code_common = { path = "advent_of_code_common" } + itertools = "0.13" regex = "1" -clap = { version = "4", features = ["derive"] } -rayon = "1.10" nalgebra = "0.33" [profile.release] diff --git a/advent_of_code_common b/advent_of_code_common new file mode 160000 index 0000000..b9fa090 --- /dev/null +++ b/advent_of_code_common @@ -0,0 +1 @@ +Subproject commit b9fa0908044042af2ca3dd66281b58afd289b4e4 diff --git a/src/main.rs b/src/main.rs index 98ac4b7..7258db4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,6 @@ -use std::{ - fs, - io::{BufRead, Read}, - time::Instant, -}; +use std::io::BufRead; -use clap::Parser; -use rayon::prelude::*; +use advent_of_code_common; mod day01; mod day02; @@ -34,21 +29,6 @@ mod day10; mod days; mod utils; -#[derive(Parser, Debug)] -#[command(author = "Greg Burri", version = "1.0", about = "Advent of Code 2024")] -struct Args { - #[arg(index(1), conflicts_with_all(["parallel"]))] - day: Option, - - /// Run all days in parallel. - #[arg(short, long)] - parallel: bool, - - /// Number of time each day is executed, the average time is displayed. - #[arg(short, long, default_value_t = 1)] - repeat: u32, -} - fn main() { println!("https://adventofcode.com/2024"); @@ -79,70 +59,5 @@ fn main() { // days::day24, ]; - let args = Args::parse(); - - match args.day { - Some(day) => { - if day >= 1 && day <= days.len() { - do_day(&days, day, args.repeat) - } else { - println!("Unknown day: {}", day) - } - } - // No argument -> execute all day problems. - None => { - let now = Instant::now(); - - if args.parallel { - (1..=days.len()) - .into_par_iter() - .for_each(|d| do_day(&days, d, args.repeat)); - } else { - (1..=days.len()).for_each(|d| do_day(&days, d, args.repeat)); - } - - println!( - "Time to execute all days: {}", - format_micros(now.elapsed().as_micros()) - ); - } - } -} - -fn do_day(days: &[fn(&mut dyn BufRead) -> String], day: usize, repeat: u32) { - let filepath = format!("data/day{:02}.input", day); - let mut f = fs::File::open(&filepath).unwrap_or_else(|error| { - println!( - "Cannot find file {}. Did you place the input files in the 'data' directory?\nError:{}", - filepath, error - ); - panic!(); - }); - - // We read the whole file to avoid measuring I/O time. - let mut buffer = Vec::new(); - f.read_to_end(&mut buffer).unwrap(); - - let now = Instant::now(); - for i in 0..repeat { - let result = days[day - 1](&mut buffer.as_slice()); - if i == repeat - 1 { - println!( - "Result of day {:02}: {} (time: {})", - day, - result, - format_micros(now.elapsed().as_micros() / repeat as u128) - ); - } - } -} - -fn format_micros(t: u128) -> String { - if t < 10_000 { - format!("{} μs", t) - } else if t < 10_000_000u128 { - format!("{:.2} ms", t as f64 / 1e3f64) - } else { - format!("{:.2} s", t as f64 / 1e6f64) - } + advent_of_code_common::run(days); }