//! # Advent of Code //! //! > **Advent of Code solutions in Rust.** #![forbid(unsafe_code)] use std::{fs::read_to_string, path::PathBuf}; use {askama::Template, clap::Parser, color_eyre::Result}; use crate::utilities::{get_solutions, session_cookie, write_file}; pub mod prelude; pub mod solution; pub mod templates; pub mod utilities; pub mod year_2021; #[derive(Debug, Parser)] #[clap(about, author, version)] pub struct Args { /// Filter to only a specific year and/or day, in `YEAR::DAY` format. pub filter: Option, } pub fn main() -> Result<()> { color_eyre::install()?; let args = Args::parse(); let filter = args.filter.unwrap_or_default(); let session_cookie = session_cookie()?; let mut years = vec![]; for year in get_solutions() { let mut solutions = vec![]; for solution in year { if !solution.day.filter_string().starts_with(&filter) { continue; } let year = solution.day.year; let day = solution.day.day; let data_path = PathBuf::from(format!("aoc/{year}/{day:02}.txt")); let data = if !data_path.exists() { println!("{data_path:?} doesn't exist, downloading"); std::thread::sleep(std::time::Duration::from_secs(1)); write_file( data_path, ureq::get(&format!( "https://adventofcode.com/{year}/day/{day}/input" )) .set("Cookie", &session_cookie) .call()? .into_string()?, )? } else { read_to_string(data_path)? }; solutions.push(solution.solve(data.trim())?); } years.append(&mut vec![solutions]); } write_file( "aoc/solutions.html".into(), templates::SolutionsTemplate { years }.render()?, )?; Ok(()) }