Extract day matching logic to lib

This commit is contained in:
Tyler Beckman 2023-12-10 21:18:27 -07:00
parent a622539277
commit 93601b8a94
Signed by: Ty
GPG key ID: 2813440C772555A4
2 changed files with 46 additions and 57 deletions

View file

@ -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<dyn Day> = 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<dyn Day> = 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::<Duration>::new();
let large = input_type == InputType::Large;

View file

@ -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<dyn Day> {
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 #"),
}
}