This is a template for writing Go solutions to Advent of Code. Each year is kept in a separate folder, where days are grouped into folders. Things that are shared between days are placed inside the internal directory.
Start by replacing all occurrences of chwallen/advent-of-code with your own module name.
The Makefile includes commands to scaffold a day. Each command requires the
variable AOC_COOKIE to be set either as an environment variable (recommended)
or passed when running a command. The YEAR and DAY variables are optional
and defaults to the current year and day.
make newscaffolds a new day, with the current year and day used as defaults.make inputdownload the input file for a day.make descriptiondownload the description for a day as Markdown.
The following section gives an example of how you would create the first day of the advent of code challenge.
Begin by retrieving your session cookie used for the AOC website. You can do so by following these steps:
- Log in to Advent of Code in your browser with your preferred method
- Open the developer panel
- Go to the Network tab and (optionally) filter by Doc
- Navigate to, or refresh, the start page
- Click the document load request
- Copy everything after after session= from the Cookie header
- Set the value as an environment variable via
export AOC_COOKIE=<value>(bash/zsh/etc.), orset -x AOC_COOKIE <value>(fish)
Now, scaffold day 1 by running make new DAY=1. This will create the folder
<year>/day01 with the files day01.go, day01_test.go, input.txt, and
description.md (change the year by setting YEAR=<other-year>). The file
day01.go will receive the input as lines []string.
To test your solution against the example input, the exampleInput in
dayXX_test.go has to be filled along with the expected value in the tests
slice. Run make test to test all your solutions. Go caches the results for
subsequent runs if the code is unchanged. In case you want to test a
specific year (or even day), set the YEAR and/or DAY variables to limit the
test pattern.
Each day part accepts extra arguments as vararg, and the Test struct has a
field for them. This is useful if an example uses a reduced problem space as it
allows your solution to dynamically change its parameters.
Use make run whenever you want to run the day you are currently working on.
Once you have solved the first part, run make description to get the
description for the second part.
You can run a specific day part by setting YEAR, DAY, and PART when
running make run. It's also possible to run all days for a specific year by
running make runall YEAR=<year>.