Liking cljdoc? Tell your friends :D

aoc-clj-elf

Clojars Project

Overview

Clojure based AOC helper.

This is a set of scripts which will help you work on Advent Of Code puzzles.

Specifically it will help you:

  • Pull puzzle input from AOC (and store to disk)
  • Generate solution files
  • Execute tests within your solutions
  • Run your solutions with the pulled input (and time your execution)

This framework also supports having multiple years together in the same repository. Your session for each year is different so you will need to add an auth for each year you want to use.

Usage

Requirements

Before you use this framework there is a pre-requisite of babashka

On a mac you can install babashka using brew:

$ brew install babashka

For other installation instructions see the official docs

Setup your Repo

In an empty folder, run:

$ bb -Sdeps '{:deps {io.github.jjcomer/aoc-clj-elf {:mvn/version "0.3.0"}}}' -m aoc-clj-elf.core init

This creates:

  • bb.edn with the library as a dependency and a single aoc task
  • .gitignore covering auth.edn (your session token) and input/ (AoC asks that puzzle inputs not be shared publicly)
  • src/ for your solutions

Running it in an existing folder is safe: existing files are left alone, and any missing .gitignore entries are appended. If you would rather set things up by hand, the generated bb.edn is:

{:paths ["src"]
 :deps {io.github.jjcomer/aoc-clj-elf {:mvn/version "0.3.0"}}
 :tasks {aoc {:doc "Advent of Code helper. Try: bb aoc help"
              :requires ([aoc-clj-elf.core :as aoc])
              :task (apply aoc/-main *command-line-args*)}}}

Every command then runs through that one task, for example bb aoc solve. Run bb aoc help to list the commands. New commands arrive with library upgrades, with no changes to bb.edn needed. If you would like more dependencies for your solutions, add them to :deps.

You can also skip the task and run bb -m aoc-clj-elf.core <command>.

Upgrading from 0.2.x

Your existing bb.edn with one task per command (bb get, bb solve, ...) keeps working. To switch to the single task, replace your :tasks with the aoc task above.

Choosing a Puzzle

Every puzzle command takes :year and :day. When left out:

  • :year defaults to the current year during December, otherwise last year's event
  • :day defaults to today's puzzle while the event is running. Outside the event you need to pass :day

Since 2025, AoC has 12 puzzles per event instead of 25, and the commands check for this.

Authentication

In order to be able to pull input for your puzzles you will need to provide your AOC session cookie value from your browser.

To find it:

  1. Log in to adventofcode.com in your browser
  2. Open the developer tools (Cmd+Option+I on macOS, F12 on Windows/Linux)
  3. Go to the Application tab (Chrome/Edge) or Storage tab (Firefox), then Cookies → https://adventofcode.com
  4. Copy the value of the session cookie

Treat this value like a password: anyone who has it can act as you on AoC.

Once you have your token, you will need to store the value for the scripts to use. This is done using the auth command:

$ bb aoc auth --year <YEAR> --session <SESSION TOKEN>

If you don't specify a year, the script will assume the current year. You can store session tokens for multiple years within the same repo.

Session tokens are stored within the auth.edn file at the root of the repo. Make sure it is in your .gitignore to prevent accidental check-ins. On macOS/Linux it is created readable by your user only.

Identifying yourself to AoC

AoC asks that automated tools say who is making requests. You can add your own contact details to the User-Agent by adding a :user-agent entry to auth.edn:

{2026 "<SESSION>"
 :user-agent "github.com/<you>/<your-aoc-repo> by <you>@example.com"}

SSL Errors and Corporate Proxies

If downloading input fails with an error like PKIX path building failed or unable to find valid certification path to requested target, something between you and adventofcode.com is presenting a certificate that babashka doesn't trust. This is usually a corporate proxy or VPN that inspects TLS traffic and re-signs it with the company's own root certificate.

Do not work around this by turning off certificate checking. The scripts send your session cookie with every request, and without certificate checks anyone who can intercept your traffic could read it. Instead, tell babashka to trust your company's root certificate:

  1. Export your company's root CA certificate as a .pem/.crt file. Your IT team can usually provide it. On macOS you can also export it from Keychain Access (find it under System or System Roots, then File → Export Items…, choosing the .pem format).

  2. Create a trust store that contains both the standard public CAs and your company's CA. keytool comes with any JDK:

    # Start from the JDK's default CA bundle so normal sites keep working
    $ cp "$(/usr/libexec/java_home)/lib/security/cacerts" ~/.java-truststore.p12   # macOS
    # (Linux: copy $JAVA_HOME/lib/security/cacerts instead)
    
    # Add your company's root CA
    $ keytool -importcert -noprompt \
        -alias corporate-root-ca \
        -file /path/to/corporate-root-ca.pem \
        -keystore ~/.java-truststore.p12 \
        -storepass changeit
    
  3. Point babashka at the trust store. Babashka accepts Java system properties as -D flags, so add a wrapper to your ~/.zshrc or ~/.bashrc:

    bb () {
      command bb -Djavax.net.ssl.trustStore="$HOME/.java-truststore.p12" \
                 -Djavax.net.ssl.trustStorePassword=changeit "$@"
    }
    

    Open a new shell (or source the file), then run bb aoc get again.

To check whether the proxy is the cause, run curl -v https://adventofcode.com 2>&1 | grep issuer. If the issuer is your company rather than a public CA such as Let's Encrypt, the proxy is re-signing the traffic.

When your company's root CA is renewed, re-run the keytool import with the new certificate.

Generating Solution Files

When you are ready to start solving a problem the framework can generate a starting file for you to use and which will be compatible with the included runners.

To create a new solution file execute the new command:

$ bb aoc new --year <YEAR> --day <DAY>

This will create a new file under src/y<YEAR>/d<DAY>.clj

Downloading Input

Your puzzle input can be downloaded directly from AOC. Using the get command you can pull any available input. With no parameters the input for today will be pulled.

$ bb aoc get --year <YEAR> --day <DAY>

If the input has already been pulled, the command will not re-pull the input. If the puzzle isn't unlocked yet, or your session has expired, the command prints a message saying so and exits non-zero.

Testing your Solution

A clojure.test runner has also been included if you want to write tests against your solution. This can be a helpful way to use the samples which are provided in the puzzles each day.

All tests in the solution's namespace will be executed. The command exits with a non-zero status if any test fails. To run your tests use the test command.

$ bb aoc test --year <YEAR> --day <DAY>

Running your Solution

When you are ready to run your solution, the framework is ready to help with the solve command. This command will check for input and automatically download if missing. The input will be passed to the generator fn within your ns. The result of the generator will be passed to both the solve-part-1 and solve-part-2 functions. The result of both functions will be printed along with how long each took. Lazy results are realised inside the timing, so a lazy generator's parsing time is reported against the generator rather than part 1. Anything your functions print (e.g. debug output) is shown as normal. If no parameters are passed today's date will be used.

$ bb aoc solve --year <YEAR> --day <DAY>

Adding Dependencies

If your solutions need any additional dependencies you can add them to the :deps map within the bb.edn. They will be pulled automatically on the next run.

Currently all solutions will run within GraalVM and more specifically the babashka runtime environment. Not all features of clojure are supported. There are a number of libraries pre-included with babashka that you can make use of with no additional configuration. See the docs for more information.

Customizations

To change the generated solution files, put a solution.template file in the root of your repo. __YEAR__ and __DAY__ are replaced with the puzzle's year and day. The default template is resources/solution.template, which is a good starting point.

Future Features

  • ~~Allow for using JVM clojure (for more speed)~~
  • Support for multiple solutions per-day
  • Switch to macros for defining generators
  • Switch to macros for defining solvers
  • Criterion (Benchmarking) integration
  • Ability to run all days sequentially
  • Optionally pprint solution output
  • More emojis ☃️
  • Better output formatting
  • Provide a filter for only running particular tests
  • Add docs for connecting REPLs

Development

Run the library's tests with bb test.

Changelog

0.3.0

  • New init command sets up a project (bb.edn, .gitignore, src/) in one step
  • Single entry point: one aoc task in bb.edn runs every command (bb aoc solve, bb aoc help, ...), so new commands no longer need bb.edn changes. The per-command task functions still work
  • Hints in messages show the command the way you run it (bb aoc new or bb new)

Unreleased -- 0.2.0

  • Security: TLS certificates are verified again. 0.1.x turned verification off, which exposed your session cookie to anyone able to intercept traffic. See SSL Errors and Corporate Proxies if you hit certificate errors
  • Fixed the User-Agent header, which was not actually being sent. You can now add your contact details with :user-agent in auth.edn
  • Year/day defaults and validation follow the AoC calendar: no more failures after the 25th, and 12 puzzles per event from 2025
  • bb auth :help prints help instead of a missing-argument error
  • Clear messages for expired sessions, puzzles that aren't unlocked yet, and missing solution files
  • bb get and bb test exit non-zero on failure
  • Lazy generator results are timed correctly, and results are printed with pr-str (so nil rather than null)
  • Debug output from solutions is no longer captured into the timing output
  • auth.edn is created readable by the owner only
  • The solution template can be overridden with a solution.template file
  • Added a deps.edn, so the library can also be used as a git or local dependency
  • Added docs on finding the session cookie

2024-12-05 -- 0.1.1

  • Use the correct timezone for AoC -- Thanks to @jacoobes

2024-11-26

  • Converted all logic to a library to make upgrading much much easier
  • Updated the README

2024-11-23

  • Changed all execution to babashka
  • Removed benchmarking as it does not work on GraalVM

2022-12-01

2022-11-30

  • Added bench task using criterium to benchmark solution time
  • Added section for generator logic to the solution template

2022-09-14

  • Changed over to execute using the JVM (will add back BB execution)
  • Breaking changed the parameters to keywords and updated documentation
  • Added .tools-version file to help choose a compatible jvm

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close