Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
In the Cloud Native ecosystem, Go has emerged as the language of choice. As a Site Reliability Engineer, knowing how to write software in Go is a useful skill.
Whether you are new to Go or have already written countless lines of it, this repository aims at giving you everything you need to improve.
2378575-c919202e
First and foremost, go to the Advent of Code website and log in.
To get started on a solution, clone this repository:
git clone [email protected]:busser/adventofcode.git
cd adventofcode
Remove existing solutions to start from scratch:
rm -r y*
Build the adventofcode
command-line tool:
make build
Get started on your first puzzle:
bin/adventofcode scaffold --day 1 --workdir "$(pwd)"
The command above will create a package where your code will go. Your next steps should be:
-
Implement your solution in the
solution.go
file. Use this command to test it:go test ./y2021/d01 -run ExamplePartOne
-
Once you think you have found the answer to the problem, submit it on the adventofcode.com website. If it's the right answer, congrats!
-
Update your tests by adding the answer to
ExamplePartOne
insolution_test.go
. -
Repeat steps 1 to 3 for the second part of the Advent of Code problem.
-
Now that you have finished, run all tests to make sure everything is ready for your pull request:
make test
Once you are done with the first day of the Advent of Code, have a look at the
Configuration section below on how you can configure the
adventofcode
CLI, or the Tests and benchmarks section
for tips on how to measure your solution's performance.
The adventofcode
can help you get started quickly on your solution to the
daily Advent of Code problems. The scaffold
subcommand builds the following
for you:
- A new package to write your solution in;
- A
solution.go
file with a basic code skeleton to get started quickly; - A
solution_test.go
file with basic unit tests and benchmarks, for when you have found the answer to the daily problem;
It can also download your input for the day's problem, granted you have provided your adventofcode.com session cookie (see Session cookie for details).
When logged in to the adventofcode.com website, your browser has a cookie called
session
. Retrieve this cookie's value and provide it to the adventofcode
CLI
to automatically download your input for the day.
This repository includes a helpers
package with useful functions for
implementing solutions to Advent of Code problems. Feel free to use any of them.
For examples on how to use them, look for functions that start with Example
.
These are actually unit tests, so you can be sure that they work as described.
The scaffolding provided by the adventofcode
CLI includes unit tests and
benchmarks. To run them, make sure they are properly uncommented in the
solution_test.go
file, then run these commands:
# Run all units tests
go test ./y2022/d01
# Run all benchmarks
go test ./y2022/d01 -bench . -benchmem -cpu 1,2,4,8
To configure the adventofcode
CLI, you can use flags, environment variables,
or a configuration file.
The CLI is entirely configurable with flags. For a list of available flags, use these commands:
adventofcode --help
adventofcode scaffold --help
You can replace any flag in the adventofcode
CLI with an environment variable.
Environment variables must simply start with ADVENTOFCODE_
. For example, you
can replace the --workdir
flag by setting the ADVENTOFCODE_WORKDIR
variable.
If you have direnv installed, you can add a .envrc
file
to the adventofcode
directory that looks like this:
export ADVENTOFCODE_WORKDIR="$(git rev-parse --show-toplevel)"
export ADVENTOFCODE_COOKIE="abcdef0123456789..."
The adventofcode
CLI automatically looks for a configuration file located in
your home directory: $HOME/.adventofcode.yaml
. In this file, you can set
values for any flag. For example, your configuration file could look like this:
workdir: /Users/arthur/workspace/adventofcode
cookie: abdefg0123456789...
If you encounter any problems while using the adventofcode
CLI, let us know
with a GitHub issue.
Made with ๐ by @busser.