summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaitep <taitep@taitep.se>2025-12-20 11:43:28 +0100
committertaitep <taitep@taitep.se>2025-12-20 11:43:28 +0100
commit86e1cd0692940c796e281450dca52f8cde1641de (patch)
tree359dc3f5befee4ab62be760c095b616531ddbe2d
init
-rw-r--r--README.md40
-rwxr-xr-xaoc-cacheclear57
-rwxr-xr-xaoc-inputdl48
3 files changed, 145 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f5f90cf
--- /dev/null
+++ b/README.md
@@ -0,0 +1,40 @@
+# aoctools
+Set of bash scripts for interacting with Advent of Code from the CLI.
+
+Aims to be minimal but functional, current set of utilities is only ~100 lines of bash and can do most of what you would need.
+
+I may add an answer uploader in the future, but I dont currently see a huge need for it.
+
+## directories
+Everything aoctools uses lives in the directory specified by `$AOC_DIR`, defaulting to `~/.aoc`.
+
+There are 2 things within here you should care about
+- `$AOC_DIR/cache`: A cache of downloaded inputs. The input for a day can be found at `cache/<year>/d<day>.in`
+- `$AOC_DIR/session`: Your session cookie, required to download your personal inputs. Should be able to be found in the devtools of your browser.
+ Do not include something like `session=` at the start, just the session key itself, should be a string of random letters and numbers.
+
+## dependencies
+Just `bash`, standard GNU/POSIX utilities (only tested on linux with GNU utils, probably works on macOS and busybox too), and `curl`.
+
+## `aoc-inputdl`
+This is the main utility you will be using to download inputs.
+
+`Usage: aoc-inputdl <year> <day>`
+
+Downloaded inputs will be automatically cached.
+
+The input data will end up in stdout, letting you view it, redirect to a file, or pipe to your solution.
+
+## `aoc-cacheclear`
+Clears your cache.
+
+```
+Usage: aoc-cacheclear <year> <day?>
+ aoc-cacheclear all
+```
+
+If passed `all`, the whole cache will be cleared.
+If only a year is passed, the full cache for that year will be cleared.
+If year and day, the cache for that day will be cleared.
+
+It is also easy to just clear it manually using `rm`, as the structure is very predictable. See [directories](#directories).
diff --git a/aoc-cacheclear b/aoc-cacheclear
new file mode 100755
index 0000000..3ce865b
--- /dev/null
+++ b/aoc-cacheclear
@@ -0,0 +1,57 @@
+#!/bin/bash
+set -euo pipefail
+
+AOC_DIR="${AOC_DIR:-$HOME/.aoc}"
+CACHE_DIR="$AOC_DIR/cache"
+
+usage() {
+ pname=$(basename "$0")
+ echo "Usage: $pname <year> <day?>" >&2
+ echo " $pname all"
+ exit 1
+}
+
+verify_int() {
+ if ! [[ "$1" =~ ^[0-9]+$ ]]; then
+ echo "'$1' is not an integer" >&2
+ usage
+ fi
+}
+
+if [ $# -gt 2 ] || [ $# -lt 1 ]; then
+ usage
+fi
+
+if [ "$1" = all ]; then
+ if [ ! -d "$CACHE_DIR" ]; then
+ echo "No cache present" >&2
+ exit 1
+ fi
+ echo "Clearing full cache" >&2
+ rm -rf "$CACHE_DIR"
+ exit
+fi
+
+year="$1"
+verify_int "$year"
+year_cache_dir="$CACHE_DIR/$year"
+
+if [ $# -eq 1 ]; then
+ if [ ! -d "$year_cache_dir" ]; then
+ echo "No cache for $year present" >&2
+ exit 1
+ fi
+ echo "Clearing cache for year $year" >&2
+ rm -rf "$year_cache_dir"
+ exit
+fi
+
+day="$2"
+verify_int "$day"
+cache_file=$year_cache_dir/d$day.in
+if [ ! -f "$cache_file" ]; then
+ echo "No cache for $year day $day present"
+ exit 1
+fi
+echo "Clearing cache for $year day $day" >&2
+rm $cache_file
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"
+