summaryrefslogtreecommitdiff
path: root/aoc-inputdl
blob: e27bfb18cfad09bfad743fe009094884bde91467 (plain)
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
#!/bin/bash
set -euo pipefail

AOC_DIR="${AOC_DIR:-$HOME/.aoc}"
CACHE_DIR="$AOC_DIR/cache"
SESSION_FILE="$AOC_DIR/session"

usage() {
    pname=$(basename "$0")
    echo "Usage: $pname <year> <day>" >&2
    exit 1
}

verify_int() {
    if ! [[ "$1" =~ ^[0-9]+$ ]]; then
        echo "'$1' is not an integer" >&2
        usage
    fi
}

if [ $# != 2 ]; then
    usage
fi

year="$1"
day="$2"

verify_int "$year"
verify_int "$day"

year_cache_dir="$CACHE_DIR/$year"
cache_file="$year_cache_dir/d$day.in"
mkdir -p "$year_cache_dir"

if [ ! -f "$cache_file" ]; then
    echo "Input not cached, downloading..." >&2
    if [ ! -f "$SESSION_FILE" ]; then
        echo "Error: No session file available" >&2
        exit 1
    fi

    session="$(cat "$SESSION_FILE")"
    url="https://adventofcode.com/$year/day/$day/input"
    curl -f "$url" -o "$cache_file" -b "session=$session"
fi

cat "$cache_file"