use advent_of_code_client::{AocClient, Level, Problem, Year};
use anyhow::anyhow;
use clap::Parser;
use colored::Colorize;
#[derive(Debug, Parser)]
#[command(author, version, about, long_about = Some(r#"Client to interact with Advent of Code. Used to submit answer for the daily puzzles.
To retrive your personal session token (varies by browser):
- Go to [adventofcode.com](https://adventofcode.com) and login
- Open the developer settings in your browser (F12)
- Go to `application` -> `Cookies`.
- You should see a session variable - this is the token we need."#
))]
struct Args {
#[arg(value_parser = clap::value_parser!(u16).range(2015..=Year::max() as i64))]
year: u16,
#[arg(value_parser = clap::value_parser!(u8).range(1..=25))]
day: u8,
#[arg(short = 'a', long)]
answer_a: Option<String>,
#[arg(short = 'b', long)]
answer_b: Option<String>,
#[arg(short = 't', long)]
token: Option<String>,
}
impl Args {
fn problem(&self) -> Result<Problem, String> {
(self.year, self.day).try_into()
}
}
fn main() -> anyhow::Result<()> {
let args = Args::parse();
let problem = args.problem().expect("Clap parser handles verification");
if args.answer_a.is_none() && args.answer_b.is_none() {
return Err(anyhow!(
"No answer provided for either part A or part B. Please provide at least one answer"
.red()
));
}
let client = args.token.map(AocClient::from_token).unwrap_or_default();
if let Some(answer) = args.answer_a {
let result = client.submit(problem, Level::A, &answer)?;
println!("{result}");
}
if let Some(answer) = args.answer_b {
let result = client.submit(problem, Level::B, &answer)?;
println!("{result}");
}
Ok(())
}