From 93601b8a94627939aebb18c4e597c359a312b8da Mon Sep 17 00:00:00 2001 From: Ty Date: Sun, 10 Dec 2023 21:18:27 -0700 Subject: [PATCH] Extract day matching logic to lib --- cli/src/main.rs | 60 +++---------------------------------------------- lib/src/lib.rs | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 7fd7e0f..883966b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,13 +1,9 @@ use std::{ - collections::HashMap, io::Write, time::{Duration, Instant}, }; -use advent::{ - day1::Day1, day10::Day10, day2::Day2, day3::Day3, day4::Day4, day5::Day5, day6::Day6, - day7::Day7, day8::Day8, day9::Day9, fetcher, utils::Day, -}; +use advent::fetcher; use clap::{Parser, Subcommand, ValueEnum}; #[derive(Parser)] @@ -97,32 +93,7 @@ async fn main() { ), }; - let mut day: Box = match day { - 1 => Box::new(Day1 { input }), - 2 => Box::new(Day2 { - input, - games: vec![], - }), - 3 => Box::new(Day3 { input }), - 4 => Box::new(Day4 { - input, - acc: 0usize, - cards: HashMap::new(), - }), - 5 => Box::new(Day5 { input }), - 6 => Box::new(Day6 { input }), - 7 => Box::new(Day7 { - input, - hands: vec![], - }), - 8 => Box::new(Day8 { input }), - 9 => Box::new(Day9 { input }), - 10 => Box::new(Day10 { - input, - parsed: vec![], - }), - _ => panic!("Invalid day #"), - }; + let mut day = advent::get_day(day, input); let start = std::time::Instant::now(); let result = day.solve(part, input_type == InputType::Large); let end = std::time::Instant::now(); @@ -166,32 +137,7 @@ async fn main() { .unwrap(), }; - let mut day: Box = match day { - 1 => Box::new(Day1 { input }), - 2 => Box::new(Day2 { - input, - games: vec![], - }), - 3 => Box::new(Day3 { input }), - 4 => Box::new(Day4 { - input, - acc: 0usize, - cards: HashMap::new(), - }), - 5 => Box::new(Day5 { input }), - 6 => Box::new(Day6 { input }), - 7 => Box::new(Day7 { - input, - hands: vec![], - }), - 8 => Box::new(Day8 { input }), - 9 => Box::new(Day9 { input }), - 10 => Box::new(Day10 { - input, - parsed: vec![], - }), - _ => panic!("Invalid day #"), - }; + let mut day = advent::get_day(day, input); let mut timings = Vec::::new(); let large = input_type == InputType::Large; diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 13b68f7..2890a06 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,3 +1,17 @@ +use std::collections::HashMap; + +use day1::Day1; +use day10::Day10; +use day2::Day2; +use day3::Day3; +use day4::Day4; +use day5::Day5; +use day6::Day6; +use day7::Day7; +use day8::Day8; +use day9::Day9; +use utils::Day; + pub mod fetcher; pub mod utils; @@ -11,3 +25,32 @@ pub mod day6; pub mod day7; pub mod day8; pub mod day9; + +pub fn get_day(day: u8, input: String) -> Box { + match day { + 1 => Box::new(Day1 { input }), + 2 => Box::new(Day2 { + input, + games: vec![], + }), + 3 => Box::new(Day3 { input }), + 4 => Box::new(Day4 { + input, + acc: 0usize, + cards: HashMap::new(), + }), + 5 => Box::new(Day5 { input }), + 6 => Box::new(Day6 { input }), + 7 => Box::new(Day7 { + input, + hands: vec![], + }), + 8 => Box::new(Day8 { input }), + 9 => Box::new(Day9 { input }), + 10 => Box::new(Day10 { + input, + parsed: vec![], + }), + _ => panic!("Invalid day #"), + } +} \ No newline at end of file