Skip to main content

Advent of Code Framework

Code style: black

https://adventofcode.com/

A framework for implementing Advent of Code solutions in Python.

Quick Start

Pip install the project so you have access to the aoc command.

pip install aoc-starter

Create an empty directory you want to write your solutions in.

aoc init

Now, you are ready to start implementing your solutions!

aoc create 01 --year 2021

This will create python file, 2021/day01.py, where you'll implement the solution.

After you've implemented the solution, you can run it with:

aoc run 01 --year 2021 -i input.txt

Solution Implementation

The file 2021/day01.py will have a concrete class, Solution01 that extends the base Solution class from aoc-starter.

There are a variety of methods that must/can be defined to best implement your solution.

Part Functions

These are the only abstract methods in the base class and therefore must be implemented.

def _part_one(self) -> int:
    ...

def _part_two(self) -> int:
    ...

These functions should return the integer answer to Advent of Code problems. The input data can be accessed using the data property, self.data.

Parsing data

There are three functions you can use to parse the data:

def _get_data(self) -> list[Any]:
    ...

def _get_data_for_part_one(self) -> list[Any]:
    ...

def _get_data_for_part_two(self) -> list[Any]:
    ...

The _get_data function is the default, so if you don't implement one of the others then _get_data is used. If there are differences in how the data should be parsed between each part you can use the more specific functions.

Inside these functions, you need to return the data as a list, down the line this return value is stored in self.data.

The input handler

The solution base class has a property input, that is of type aoc.InputHandler. This object is the intermediary between the raw data and the solution. When parsing data, you can use the self.input.as_list function to interact with the raw content.

# default, return the content as a list of strings
def _get_data(self) -> list[str]:
    return self.input.as_list()

# parse as a certain type
def _get_data(self) -> list[int]:
    return self.input.as_list(int)

# use a custom parser
def _get_data(self) -> list[tuple[str, int]]:
    def parser(content: str, **kwargs) -> tuple[str, int]:
        key, val = content.split(": ")
        return key, int(val)

    return self.input.as_list(parser)

Reformatting input file

You can use the _reformat_data function to change how the input data looks.

Pop Lines

Sometimes the input file has a line at the beginning that is different from the actual data. The InputHandler has a method pop_line that removes and returns the first line in raw content. If you want to use this before any reformats happen, you can override the _pop_lines method in the solution.

class Day01(Solution):

    first_line: str

    def _pop_lines(self) -> None:
        self.first_line = self.input.pop_line()

Release files for aoc-starter 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aoc-starter 0.2.0
File Size Uploaded
aoc-starter-0.2.0.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aoc-starter 0.2.0
File Interpreter ABI Platform
aoc_starter-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.6 kB

Release files / aoc-starter-0.2.0.tar.gz

Download URL aoc-starter-0.2.0.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1826df15c7413fdc2b77ab7d8b82d6fe5c7f461204c9e45416eee3209aeb1964
BLAKE2b-256 checksum
How to use checksums
f320cf30a2d5bdb4e17d0f7a41ad7a1026574c1572454c041f179fc87cbab5b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release files / aoc_starter-0.2.0-py3-none-any.whl

Download URL aoc_starter-0.2.0-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
245fae125253f1581a373e6f4e3b2ca4bb61945ecaebc7d1dc296a8dee274c74
BLAKE2b-256 checksum
How to use checksums
b222372819265c61c2a0cef4921fadaf862ff2adcda6663888583924cf3921cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.3

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page