1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
mod error;
use std::{io::Write, path::PathBuf};

type AocResult<T> = Result<T, error::Error>;

pub fn get_puzzle_input(year: u32, day: u32, session: String) -> AocResult<String> {
    from_cache(year, day)
        .ok_or(())
        .or_else(|_| from_web(year, day, session, true))
}

pub fn from_web(year: u32, day: u32, session: String, should_cache: bool) -> AocResult<String> {
    let url = format!("https://adventofcode.com/{}/day/{}/input", year, day);

    let cookie = format!("session={}", session);

    let puzzle_input = reqwest::blocking::Client::new()
        .get(url)
        .header("Cookie", cookie)
        .send()?
        .text()?;

    if should_cache {
        set_cache(year, day, puzzle_input.clone());
    }
    Ok(puzzle_input)
}

// directory structure:
// ~/.aoc_puzzles/{year}/day-{day}.txt
fn get_path(year: u32, day: u32) -> Option<PathBuf> {
    home::home_dir().map(|mut h| {
        h.push(format!(".aoc_puzzles/{year}/day-{day}.txt"));
        h
    })
}

/// Try to get the puzzle from the cache.
fn from_cache(year: u32, day: u32) -> Option<String> {
    get_path(year, day).and_then(|p| std::fs::read_to_string(p).ok())
}

/// Try to cache the puzzle input under the home directory.
/// Discard any errors if caching fails
fn set_cache(year: u32, day: u32, puzzle_input: String) {
    if let Some(p) = get_path(year, day) {
        // should not fail
        let prefix = p.parent().unwrap();

        _ = std::fs::create_dir_all(prefix);
        _ = std::fs::File::create(p).and_then(|mut f| f.write_all(puzzle_input.as_bytes()));
    };
}