Advent of Code problems solved in Python.
Dazbo's Advent of Code
See my walkthroughs here.
Overview
- My solutions to Advent of Code problems for multiple years, written in Python.
- All solutions are thoroughly documented.
- Some days have multiple solutions where I've experimented with different approaches or libraries.
Purpose of this Repo
I've been using Advent of Code as a way to improve my Python skills. Here I document my solutions across the various AoC years. My hope is that this guide will help others to:
- Become more proficient with Python.
- Solve AoC problems they might be stuck with.
- Learn some new libraries and inventive ways of doing things.
Use of my Code
This software is shared as open source. However, if you use it in your own solutions and/or incorporate into your own repos, please consider giving acknowledgement to me, and linking back to this repo and walkthrough site. That would be really kind!
What is Advent of Code?
An awesome coding challenge created by Eric Wastl, released every December. A new problem is presented each day through the month. Typically the best way to solve any given problem is by writing a program. The program can be written in any language and with any tools you like_. You don't need to be an expert coder to do AoC; in fact, AoC is a great way to learn a programming language.
Some problems are quite trivial and can be solved quickly; others can be a total PITA. Typically, the problems get harder as the month progresses.
You don't have to wait until December to try your hand at AoC though. All the previous events are available, and can be completed at any time.
Each day is split into a Part 1 and a Part 2. A star is awarded for each completed challenge.
If you get 50 stars, you save Christmas!
Prerequisites & Setup
To run the solutions in this repository, you'll need the following:
- Python >=3.13
uv: My preferred tool for managing Python packages and virtual environments.- Jupyter: For running the notebook-based solutions (
.ipynbfiles).
Installation
- Clone the repository:
git clone https://github.com/derailed-dash/Advent-of-Code.git
cd Advent-of-Code
- Create a virtual environment and install dependencies:
uv to create a virtual environment and install the required packages from pyproject.toml.
# Install dependencies in a venv, including Jupyter
make install # runs uv sync --dev --extra jupyter
Alternatively, you can use conda as described in src/readme.md.
- Run the solutions:
uv run src/AoC2022/d01caloriecounting/elfcalories.py
- For Jupyter Notebooks, run them directly in your IDE (e.g. VS Code), start a Jupyter Lab,
or run in a cloud service like Google Colab.
# Running a Jupyter lab locally
jupyter lab
Then navigate to the notebook file in the src/AoC_YYYY directory.
Structure and Solution Approaches
This repository is organized by year and solution type. I've taken two main approaches for my solutions:
1. Python Scripts with Separate Walkthroughs
For some years (e.g., 2015-2022), the solutions are plain Python scripts. These are accompanied by detailed walkthroughs written in Markdown, which are published on the website.
- Code: The Python solution files (
.py) are located in thesrc/AoC_YYYY/directories. - Walkthroughs: The corresponding Markdown files (
.md) are in thedocs/YYYY/directories. These are used by Jekyll to build the static website.
2. Jupyter Notebooks
For other years (e.g., 2023 onwards), I've used Jupyter Notebooks. This approach combines the Python code, the solution explanation, and the walkthrough into a single, self-contained file (.ipynb). These can be viewed directly on GitHub or run locally using Jupyter.
Directory Structure
Here is a simplified overview of the repository's structure, showing examples of both solution types:
.
ββββdocs/ # Source for the Jekyll-based walkthrough website
β ββββ2022/ # Walkthroughs for 2022 (Python script approach)
β β ββββ1.md # Walkthrough Day 1
β python/ # General Python guides
β β ββββassertion.md # Walkthrough for a specific topic
| | ββββ...
| ββββ_config.yml # Jekyll configuration for building docs
| ββββ_config.docker.yml # Jekyll configuration for Docker
| ββββdocker-compose.yml # Docker configuration
| ββββindex.md # Index page for the website
β ββββ...
ββββsrc/ # All Python source code and notebooks
β ββββAoC_2022/ # Solutions for 2022
β | ββββd01/ # Day 1
| | | ββββinput/ # Input data
| | | ββββd01.py # Solution for 2022, Day 1
| | ββββd02/ # Day 2
| | ββββ...
β ββββAoC_2023/
β β ββββDazbo'sAdventofCode2023.ipynb # Walkthroughs for 2023 (Jupyter Notebook approach)
β | ββββd01/ # Day 1
| | | ββββinput/ # Input data
β | ββββd02/ # Day 2
| | | ββββinput/ # Input data
| | ββββ...
β ββββaoc_common/ # A shared library of common functions used across solutions
β ββββtemplate_folder/ # Template content
β ββββtests/ # Unit tests
ββββscripts/ # Helper scripts for repository management
ββββ.env # Env vars, e.g. AoC session cookie
ββββ.gitignore
ββββLICENSE
ββββREADME.md # This file
ββββpyproject.toml # Project metadata and dependencies for uv/pip
Helper Libraries
This repository leverages two primary utility libraries to streamline solution development:
-
aoccommon(local to this repository): Located insrc/aoccommon/, this library provides Advent of Code-specific utilities such as:
Point, Vectors, VectorDicts, and Grid classes for handling 2D grid-based problems.
- binarysearch, mergeintervals, getfactors, and tobase_n for common algorithmic tasks.
- timer context manager for performance measurement.
- setupfilelogging for logging output to files.
-
dazbo-commons(external PyPI package): This is a more generic utility library that provides foundational functionalities used across my Python projects. It is installed as a dependency and offers:
By separating these concerns, aoc_common remains focused on AoC-specific helpers, while dazbo-commons handles more general-purpose tasks, promoting reusability and cleaner code.
Note that the aoc_common package is imported into standalone scripts, but it's code is replicated in the notebooks. This is so that the notebooks can be entirely portable.
Assertions and Testing
Each solution typically includes assertions to verify correctness, especially against sample inputs provided by Advent of Code. The aoc_common.validate function is used for this purpose:
ac.validate(testresult, expectedanswer)
This function raises an AssertionError if the testresult does not match the expectedanswer, providing immediate feedback during development and ensuring the solution works for known cases before attempting the full input.
Helper Scripts
The scripts/ directory contains helper scripts for managing this repository:
create_year.ps1: A PowerShell script to scaffold the directory structure for a new Advent of Code year.
Documentation Website
This repository includes a companion website that contains detailed walkthroughs and explanations for many of the solutions.
- Website URL: https://aoc.just2good.co.uk/
- Technology: The site is a static website generated using Jekyll.
- Source Code: All source files for the website (Markdown content, layouts, etc.) are located in the
docs/directory.
Content Structure
- For solutions written as plain Python scripts, the corresponding walkthroughs are individual Markdown files found in
docs/YYYY/, whereYYYYis the year of the challenge. - For solutions implemented in Jupyter Notebooks, the code and walkthrough are combined in the
.ipynbfile itself, located in thesrc/AoC_YYYY/directory.
Working with the Documentation Locally
You can build and serve the documentation website on your local machine using Docker.
- Navigate to the
docs/directory. - Run the command
docker compose up. - The website will be available at
http://127.0.0.1:4000.
.env file in the docs/ directory containing JEKYLLGITHUBTOKEN=yourtokenhere.