Skip to content

Commit

Permalink
feat: allow do download input
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Nov 30, 2024
1 parent 681ec65 commit e6c9c39
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SESSION_COOKIE_ENV_VAR=
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
.idea
/resources/*/inputs/*.in
/resources/*/outputs/*.out
Cargo.lock
Cargo.lock
.env
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
aoc-client = "0.2.0"
clap = { version = "4.5.21", features = ["derive"]}
dotenv = "0.15.0"
regex = "1.11.1"
itertools = "0.13.0"
41 changes: 40 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use crate::utils::year::Year;
use clap::{Parser, Subcommand};
use std::fmt::{Display, Formatter};
use std::time::{Duration, Instant};
use aoc_client::{AocClient};
use dotenv::dotenv;
use utils::day_number::DayNumber;
use utils::file_system::{read_input, read_output};
use utils::year::Year::Year2023;
use crate::utils::file_system::write_input;

mod solutions;
mod utils;
Expand All @@ -24,8 +27,12 @@ struct Args {

#[derive(Subcommand, Debug)]
enum Command {
/// Run solver for given puzzle
#[clap(short_flag = 's')]
Solve,
/// Downloads and saves input for given puzzle
#[clap(short_flag = 'i')]
Input,
}

fn parse_day(s: &str) -> Result<u8, String> {
Expand All @@ -37,6 +44,8 @@ fn parse_day(s: &str) -> Result<u8, String> {
}

fn main() {
dotenv().ok();

let cli = Args::parse();
let command = cli.command.unwrap_or(Command::Solve);
let day = cli.day.unwrap_or(1);
Expand All @@ -48,20 +57,26 @@ fn main() {

match command {
Command::Solve => solve(&day_number, year),
Command::Input => download_input(day_number, year),
}
}

fn solve(day_number: &DayNumber, year: Year) {
let solution = solution(&day_number, year.clone());

let input = read_input(day_number.to_string().as_str(), year.clone());
let input = match read_input(day_number.to_string().as_str(), year.clone()) {
Ok(val) => val,
Err(_) => panic!("Failed to read input. Download it first."), // todo better handle errors
};

let output = read_output(day_number.to_string().as_str(), year);

let expected: Vec<String> = output
.unwrap_or(String::from(""))
.lines()
.map(|s| s.to_string())
.collect();

let expected_part_one = expected.first();
let expected_part_two = expected.get(1);

Expand All @@ -75,6 +90,30 @@ fn solve(day_number: &DayNumber, year: Year) {
);
}

fn download_input(day_number: DayNumber, year: Year) {
let input = read_input(day_number.to_string().as_str(), year.clone());

match input {
Ok(_) => println!("Input already exists."),
Err(_) => {
println!("Downloading...");
let session = std::env::var("SESSION_COOKIE_ENV_VAR").unwrap();

let client = AocClient::builder()
.session_cookie(session).unwrap()
.year(year.clone() as i32).unwrap()
.day(u32::from(day_number)).unwrap()
.build().unwrap();

let input = client.get_input().unwrap();

write_input(&day_number.to_string(), year.clone(), &input).unwrap();

println!("Input downloaded");
}
}
}

fn run<'a>(
part: &str,
solve_fn: &'a dyn Fn() -> String,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/day_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ impl From<DayNumber> for u8 {
}
}

impl From<DayNumber> for u32 {
fn from(day: DayNumber) -> Self {
day.number as u32
}
}

impl Display for DayNumber {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0>2}", self.number)
Expand Down
24 changes: 21 additions & 3 deletions src/utils/file_system.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::year::Year;
use std::fmt::{Display, Formatter};
use std::fs;
use std::fs::read_to_string;

enum ResourceType {
Expand All @@ -22,8 +23,14 @@ impl Display for ResourceType {
}
}

pub fn read_input(day: &str, year: Year) -> String {
read(ResourceType::Inputs, day, year).unwrap()
pub fn write_input(day: &str, year: Year, data: &str) -> std::io::Result<()> {
let file_path = build_path(ResourceType::Inputs, day, year);

fs::write(file_path, data)
}

pub fn read_input(day: &str, year: Year) -> std::io::Result<String> {
read(ResourceType::Inputs, day, year)
}

pub fn read_output(day: &str, year: Year) -> std::io::Result<String> {
Expand All @@ -36,7 +43,18 @@ pub fn read_example(day: &str, year: Year) -> String {
}

fn read(resource_type: ResourceType, day: &str, year: Year) -> std::io::Result<String> {
let file_path = format!("resources/{}/{}/{}.in", year, resource_type, day);
let file_path = build_path(resource_type, day, year);

read_to_string(file_path)
}

fn build_path(resource_type: ResourceType, day: &str, year: Year) -> String {
let format = match resource_type {
ResourceType::Inputs => "in",
ResourceType::Outputs => "out",
#[cfg(test)]
ResourceType::Examples => "in",
};

format!("resources/{}/{}/{}.{}", year, resource_type, day, format)
}

0 comments on commit e6c9c39

Please sign in to comment.