Advent of Code puzzle input downloader

TL;DR

A shell program to download inputs for Advent of Code puzzles.

This is probably a note to myself for 2023.

It needs to access the cookie session from an authenticated browser. In Firefox:

  • open Advent of Code and open the Developer Tools
  • go to the Network tab
  • login if you haven’t yet, refresh otherwise
  • look for cookie session in one of the fetched items
  • save it in /path/to/local/session-cookie

You can call it in different ways:

/path/to/get-inputs.sh 2018 15      # 15th puzzle of 2018 edition

# in a directory named 2022
/path/to/get-inputs.sh 4            # 4th puzzle of 2022 edition

# in a directory named 2022, on December 13th
/path/to/get-inputs.sh              # 13th puzzle of 2022 edition

Here’s the code:

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
#!/bin/sh
me="$(readlink -f "$0")"
md="$(dirname "$me")"
read session <"$md/local/session-cookie"

if [ $# -eq 2 ] ; then
   year="$1"
   shift
else
   year="$(basename "$PWD")"
   if printf %s "$year" | grep -v '^20[0-9][0-9]$' >/dev/null 2>&1 ; then
      year="$(date +%Y)"
   fi
fi

if [ $# -eq 1 ] ; then
   day="$(printf %s "$1" | sed -e 's/^0//')"
   day2="$(printf %s "0$day" | sed -e 's/^.*\(..\)$/\1/')"
else
   read  day day2 <<END
$(date +'%e  %d')
END
fi

printf %s\\n "$day2"

blogurl='https://etoobusy.polettix.it/2022/11/28/aoc-inputs-downloader/'
curl -sL --cookie "session=$session" \
   -H "User-Agent: $blogurl" \
   "https://adventofcode.com/$year/day/$day/input" \
   | tee "$day2.input"

Enjoy!


Comments? Octodon, , GitHub, Reddit, or drop me a line!