diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-11-29 14:52:27 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-11-29 14:55:03 +0100 |
| commit | 5c6a17174f6675d01bf35999b025c0e99297a844 (patch) | |
| tree | 3671a8f8a9b60d46386247a8a291fb6ab388e4a8 /src | |
| parent | a4d6defa1722d52bf0f5bb2f3abf1443c4b6509c (diff) | |
| download | sebastiano.tronto.net-5c6a17174f6675d01bf35999b025c0e99297a844.tar.gz sebastiano.tronto.net-5c6a17174f6675d01bf35999b025c0e99297a844.zip | |
Blog post
Diffstat (limited to 'src')
| -rw-r--r-- | src/blog/2025-11-29-aoc-download/aoc-download.md | 88 | ||||
| -rw-r--r-- | src/blog/2025-11-29-aoc-download/cookie.png | bin | 0 -> 76516 bytes |
2 files changed, 88 insertions, 0 deletions
diff --git a/src/blog/2025-11-29-aoc-download/aoc-download.md b/src/blog/2025-11-29-aoc-download/aoc-download.md new file mode 100644 index 0000000..5190aa9 --- /dev/null +++ b/src/blog/2025-11-29-aoc-download/aoc-download.md | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | # Downloading AoC puzzle inputs with curl | ||
| 2 | |||
| 3 | As in the [past couple of years](../2023-12-25-advent-of-code), soon | ||
| 4 | I am going to take part in the daily programming challenges of the | ||
| 5 | [Advent of Code](https://adventofcode.com). This time I decided to take | ||
| 6 | some time and optimize my working setup a little bit - not that it matters | ||
| 7 | for someone solving the problems at my speed. | ||
| 8 | |||
| 9 | So let me show you how I automated downloading the inputs for the puzzles | ||
| 10 | (which require you to login) using [curl](https://curl.se). | ||
| 11 | |||
| 12 | ## The problem | ||
| 13 | |||
| 14 | In case you are not familiar with the Advent of Code, it is a casual | ||
| 15 | programming challenge that takes place every year in December. Every day | ||
| 16 | a new problem is released, and you are supposed to write a program that | ||
| 17 | solves it and submit your answer as a short string, usually a number. | ||
| 18 | The fastest you solve the problems, the more internet points | ||
| 19 | you get, and you can brag about it with your colleagues. | ||
| 20 | |||
| 21 | The input data for each problem is given as a simple text file. Not | ||
| 22 | everyone gets the same, there are a few (hundreds? thousands? not sure) | ||
| 23 | of different inputs, and each user is assigned one. | ||
| 24 | |||
| 25 | This means that one must log in to be able to download their puzzle input. | ||
| 26 | To manually download the input file is quite tedious, so it would be nice | ||
| 27 | if there was a way to automate this. Of course, | ||
| 28 | [I am](https://github.polettix.it/ETOOBUSY/2022/11/28/aoc-inputs-downloader) | ||
| 29 | [not](https://packagist.org/packages/colinodell/aoc-downloader) | ||
| 30 | [the](https://github.com/johlinco/aoc-cli) | ||
| 31 | [first](https://github.com/Gronner/aoc-downloader) | ||
| 32 | [to](https://github.com/czyber/aoc-downloader) | ||
| 33 | [think](https://github.com/martintc/aoc-downloader-rs) | ||
| 34 | [about](https://github.com/scarvalhojr/aoc-cli) | ||
| 35 | [doing](https://github.com/colinodell/aoc-downloader) | ||
| 36 | [this](https://github.com/GreenLightning/advent-of-code-downloader). | ||
| 37 | |||
| 38 | ## Session cookies | ||
| 39 | |||
| 40 | If log in was not required, we could simply download the input file for, | ||
| 41 | say, day 12 of 2024 and save it to `input.txt` with curl like this: | ||
| 42 | |||
| 43 | ``` | ||
| 44 | $ curl https://adventofcode.com/2024/day/12/input -o input.txt | ||
| 45 | ``` | ||
| 46 | |||
| 47 | But if you run this command, you'll get something like this: | ||
| 48 | |||
| 49 | ``` | ||
| 50 | Puzzle inputs differ by user. Please log in to get your puzzle input. | ||
| 51 | ``` | ||
| 52 | |||
| 53 | To make curl act as if we were logged in, we need a | ||
| 54 | [session cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies). | ||
| 55 | If you log into [adventofcode.com](https://adventofcode.com) on a browser such | ||
| 56 | as Firefox, you can right-click on the page, select "Inspect" and then navigate | ||
| 57 | to Storage and then Cookies. | ||
| 58 | |||
| 59 |  | ||
| 60 | |||
| 61 | The long alphanumerical string under "Value" is your session cookie. Anyone | ||
| 62 | who gets a hold of it can pretend to be you on this website - by the way, | ||
| 63 | this is how the famous YouTube channel | ||
| 64 | [Linus tech tips got hacked](https://www.youtube.com/watch?v=yGXaAWbzl5A) | ||
| 65 | a couple of years ago. | ||
| 66 | |||
| 67 | Save this cookie somewhere, for example in an | ||
| 68 | [environment variable](https://en.wikipedia.org/wiki/Environment_variable) | ||
| 69 | called `AOC_SESSION_COOKIE`. Then run your curl with an extra option, like so: | ||
| 70 | |||
| 71 | ``` | ||
| 72 | $ curl https://adventofcode.com/2024/day/12/input -H "cookie: session=$AOC_SESSION_COOKIE" -o input.txt | ||
| 73 | ``` | ||
| 74 | |||
| 75 | And you are good to go! | ||
| 76 | |||
| 77 | ## The fancy script | ||
| 78 | |||
| 79 | Typing out the full command above may look cooler than downloading the file | ||
| 80 | by right-clicking "Save page as", but it is just as annoying. So obviously I | ||
| 81 | saved that command to | ||
| 82 | [a script](https://git.tronto.net/scripts/file/aoc.html) | ||
| 83 | that also detects the correct file to download based on the current date | ||
| 84 | (or on two optional parameters for the year and the day). My script also | ||
| 85 | opens up an editor and [Lynx](https://en.wikipedia.org/wiki/Lynx_(web_browser)) | ||
| 86 | session to set me up to speed, but that is not as interesting. | ||
| 87 | |||
| 88 | Happy coding! | ||
diff --git a/src/blog/2025-11-29-aoc-download/cookie.png b/src/blog/2025-11-29-aoc-download/cookie.png new file mode 100644 index 0000000..947742f --- /dev/null +++ b/src/blog/2025-11-29-aoc-download/cookie.png | |||
| Binary files differ | |||
