diff options
| author | taitep <taitep@taitep.se> | 2025-12-20 11:43:28 +0100 |
|---|---|---|
| committer | taitep <taitep@taitep.se> | 2025-12-20 11:43:28 +0100 |
| commit | 86e1cd0692940c796e281450dca52f8cde1641de (patch) | |
| tree | 359dc3f5befee4ab62be760c095b616531ddbe2d /aoc-inputdl | |
init
Diffstat (limited to 'aoc-inputdl')
| -rwxr-xr-x | aoc-inputdl | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/aoc-inputdl b/aoc-inputdl new file mode 100755 index 0000000..e27bfb1 --- /dev/null +++ b/aoc-inputdl @@ -0,0 +1,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" + |
