1
Fork 0
advent-of-code/source/main.rs

75 lines
1.7 KiB
Rust
Raw Normal View History

//! # 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;
2022-10-03 16:02:40 +00:00
pub mod year_2020;
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<String>,
}
pub fn main() -> Result<()> {
2021-12-01 11:50:38 +00:00
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]);
}
2021-12-04 13:38:34 +00:00
write_file(
"aoc/solutions.html".into(),
templates::SolutionsTemplate { years }.render()?,
)?;
2021-12-01 11:50:38 +00:00
Ok(())
}