AoC-2023/src/main.rs

53 lines
2.3 KiB
Rust
Raw Normal View History

2023-12-02 00:39:37 +01:00
pub mod input;
2023-12-05 12:36:28 +01:00
pub mod error;
2023-12-02 00:39:37 +01:00
2023-12-05 14:50:36 +01:00
pub mod days;
use days::*;
2023-12-05 10:51:08 +01:00
2023-12-05 12:36:28 +01:00
use std::env;
2023-12-02 00:39:37 +01:00
2023-12-05 12:36:28 +01:00
use crate::error::AdventError;
2023-12-02 00:39:37 +01:00
fn main() -> Result<(), AdventError> {
2023-12-05 10:51:08 +01:00
let days = vec![
(day1::one as fn() -> Result<u64, AdventError>, day1::two as fn() -> Result<u64, AdventError>),
(day2::one as fn() -> Result<u64, AdventError>, day2::two as fn() -> Result<u64, AdventError>),
(day3::one as fn() -> Result<u64, AdventError>, day3::two as fn() -> Result<u64, AdventError>),
(day4::one as fn() -> Result<u64, AdventError>, day4::two as fn() -> Result<u64, AdventError>),
(day5::one as fn() -> Result<u64, AdventError>, day5::two as fn() -> Result<u64, AdventError>),
(day6::one as fn() -> Result<u64, AdventError>, day6::two as fn() -> Result<u64, AdventError>),
(day7::one as fn() -> Result<u64, AdventError>, day7::two as fn() -> Result<u64, AdventError>),
(day8::one as fn() -> Result<u64, AdventError>, day8::two as fn() -> Result<u64, AdventError>),
2023-12-11 19:41:17 +01:00
(day9::one as fn() -> Result<u64, AdventError>, day9::two as fn() -> Result<u64, AdventError>),
2023-12-13 20:02:33 +01:00
(day10::one as fn() -> Result<u64, AdventError>, day10::two as fn() -> Result<u64, AdventError>),
2023-12-13 21:22:12 +01:00
(day11::one as fn() -> Result<u64, AdventError>, day11::two as fn() -> Result<u64, AdventError>),
2023-12-05 10:51:08 +01:00
];
2023-12-07 15:34:46 +01:00
if env::args().len() == 1 {
2023-12-05 10:51:08 +01:00
Ok(days.into_iter().map(|(one, two)| {
let one_result = one()?;
let two_result = two()?;
Ok((one_result, two_result))
}).enumerate().map(|(index, r): (usize, Result<(u64, u64), AdventError>)| {
2023-12-05 10:51:08 +01:00
match r {
Ok((res1, res2)) => println!("{}: ({}, {})", index + 1, res1, res2),
2023-12-05 10:51:08 +01:00
Err(e) => println!("Error: {}", e.0)
};
}).fold((), |acc, _| { acc }))
} else {
2023-12-07 15:34:46 +01:00
let day_index = env::args().nth(1).ok_or(AdventError("Failed to parse day".into()))?.parse::<usize>()? - 1;
let (one, two) = days[day_index];
if env::args().len() == 3 {
match env::args().nth(2).unwrap().parse::<usize>()? {
1 => Ok(println!("1: {}", one()?)),
2 => Ok(println!("2: {}", two()?)),
_ => return Err(AdventError("part must be either 1 or 2".into()))
}
} else {
Ok(println!("{}: ({}, {})", day_index + 1, one()?, two()?))
2023-12-05 10:51:08 +01:00
}
}
2023-12-02 00:39:37 +01:00
}