From 3bff18f34f703901903be3f3bb384282b9820c91 Mon Sep 17 00:00:00 2001 From: Spectre Date: Tue, 3 Dec 2024 02:47:17 +0100 Subject: [PATCH] setup --- aoc2023/src/day01.rs | 20 ++++++++------------ examples/2024/day01 | 6 ++++++ examples/2024/day01_solution | 2 ++ examples/2024/day02 | 6 ++++++ examples/2024/day02_solution | 2 ++ src/input_data.rs | 9 ++++++--- src/main.rs | 25 ++++++++++++++++++------- 7 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 examples/2024/day01 create mode 100644 examples/2024/day01_solution create mode 100644 examples/2024/day02 create mode 100644 examples/2024/day02_solution diff --git a/aoc2023/src/day01.rs b/aoc2023/src/day01.rs index 49cc0c6..263006b 100644 --- a/aoc2023/src/day01.rs +++ b/aoc2023/src/day01.rs @@ -1,5 +1,5 @@ use shared::{Answer, Solution}; -use regex::{Regex}; +use regex::Regex; pub struct Day01; @@ -22,7 +22,7 @@ impl Solution for Day01 { } } - sum += 10 * first.expect(word) + last.expect(word) + sum += 10 * first.unwrap_or(0) + last.unwrap_or(0) } Answer::from(sum) @@ -40,23 +40,19 @@ impl Solution for Day01 { ).unwrap(); Answer::from(words.iter() - .map(|word| {10 * matches(&first.captures(&word).unwrap()) + matches(&last.captures(word).unwrap())}) + .map(|word| {10 * matches(first.captures(word)) + matches(last.captures(word))}) .collect::>() .iter() .sum::()) } } -fn matches(cap: ®ex::Captures) -> u64 { +fn matches(cap: Option) -> u64 { + if cap.is_none() { return 0} ; + for i in 1..10 { - if cap.get(i).is_some() { return i as u64 }; + if cap.as_ref().unwrap().get(i).is_some() { return i as u64 }; } - return 0 -} - -#[test] -fn example() { - assert_eq!(Day01.part_1(load_test(2023, 1)?.as_str()), 142); - assert_eq!(Day01.part_2(load_test(2023, 1)?.as_str()), 281); + 0 } diff --git a/examples/2024/day01 b/examples/2024/day01 new file mode 100644 index 0000000..b8af9ad --- /dev/null +++ b/examples/2024/day01 @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 diff --git a/examples/2024/day01_solution b/examples/2024/day01_solution new file mode 100644 index 0000000..4cb4249 --- /dev/null +++ b/examples/2024/day01_solution @@ -0,0 +1,2 @@ +11 +31 diff --git a/examples/2024/day02 b/examples/2024/day02 new file mode 100644 index 0000000..b49c10d --- /dev/null +++ b/examples/2024/day02 @@ -0,0 +1,6 @@ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 diff --git a/examples/2024/day02_solution b/examples/2024/day02_solution new file mode 100644 index 0000000..da7f847 --- /dev/null +++ b/examples/2024/day02_solution @@ -0,0 +1,2 @@ +2 +4 diff --git a/src/input_data.rs b/src/input_data.rs index 131bbf6..6179c91 100644 --- a/src/input_data.rs +++ b/src/input_data.rs @@ -2,11 +2,14 @@ use std::{fs, io}; pub fn load_actual(year: usize, day: usize) -> io::Result { let path = format!("./data/{year}/day{:02}", day); - fs::read_to_string(path) + let file = fs::read_to_string(path)?; + Ok(file.trim().into()) } -pub fn load_test(year: usize, day: usize) -> (io::Result, io::Result) { +pub fn load_test(year: usize, day: usize) -> Result<(String, String), io::Error> { let input_path = format!("./examples/{year}/day{:02}", day); let result_path = format!("./examples/{year}/day{:02}_solution", day); - (fs::read_to_string(input_path), fs::read_to_string(result_path)) + let out1 = fs::read_to_string(input_path)?; + let out2 = fs::read_to_string(result_path)?; + Ok((out1.trim().into(), out2.trim().into())) } diff --git a/src/main.rs b/src/main.rs index 563d75f..bffc73f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,21 +10,25 @@ use fetch_input::fetch_input; use std::{path::Path, time::Instant, env}; fn main() { + run_daily(); +} + +fn run_daily() { let year = env::var("ADVENT_YEAR") - .unwrap_or("2024".into()) + .unwrap() .parse() .unwrap(); let day = env::var("ADVENT_DAY") - .unwrap_or("1".into()) + .unwrap() .parse() .unwrap(); - if ! Path::new(&format!("./data/{}/day{:02}", year, day)).exists() - && env::var("ADVENT_TOKEN").is_ok() { - fetch_input(year, day).expect("Set ADVENT_TOKEN to the correct session cookie to fetch input automatically"); - } + fetch_input(year, day).expect("Token Failed"); - run_all(); + let data = load_actual(year, day).unwrap(); + + println!("Solved 1: {}", solution(&data, year, day, 1).unwrap()); + println!("Solved 2: {}", solution(&data, year, day, 1).unwrap()); } fn run_all() { @@ -38,8 +42,11 @@ fn run_all() { } } } + for year in years_to_run { + let mut total = 0; + for day in 1..=25 { for part in [1, 2] { let input = load_actual(year, day).unwrap_or_else(|_| panic!("Missing input for {}/{:02}", year, day)); @@ -47,8 +54,12 @@ fn run_all() { let now = Instant::now(); let result = solution(&input, year, day, part).unwrap_or_else(|| panic!("No solution at {}/{:02}", year, day)); let time_passed = now.elapsed().as_millis(); + + total += time_passed; + println!("Running Year {}, Day {:02}, Part {} - Result {} - took {}ms", year, day, part, result, time_passed); } } + println!("\nTook a total of {total}ms to run {year}!"); } }