A structural template and runner for Advent of Code solutions written in Python.
Project description
This project provides a structural template and runner for Advent of Code solutions written in Python. It is built on top of jogger (https://github.com/oogles/task-jogger).
Installation
The latest stable version can be installed from PyPI:
pip install task-jogger-aoc
The following dependencies will also be installed:
jogger: The underlying task running system. See: https://github.com/oogles/task-jogger.
Quickstart
Add a jog.py file including AdventOfCodeTask:
from aoc.tasks import AdventOfCodeTask
tasks = {
'aoc': AdventOfCodeTask
}
Assuming a task name of aoc, as used in the above example, create a template for solving the next puzzle using:
$ jog aoc --next or $ jog aoc -n
This will create a subdirectory for the next day’s puzzle that doesn’t already have one, using day 1 by default. E.g. solutions/day01/. This subdirectory contains:
solvers.py: A module containing part1() and part2() functions for solving the respective parts of the puzzle (it’s up to you to implement these). It also optionally contains an input_parser function (see Input parsing below).
input: A file to contain the puzzle input. This can be populated automatically if you have a session cookie set (see Configuration below). Otherwise, it must be populated manually.
Once at least one solver is implemented, the latest puzzle can be run using:
$ jog aoc
To run the solvers using sample data instead of the full input data, use:
$ jog aoc --sample or $ jog aoc -s
When using sample data, you will be prompted to enter separate sample data for each part of the puzzle. Part 2 will offer the choice to use the same sample data as part 1. Entered sample data will be saved to solutions/day01/sample1 and solutions/day01/sample2, respectively, and the prompt will not be shown again.
Each part of the puzzle can be run independently using:
$ jog aoc --part1 or $ jog aoc -1 $ jog aoc --part2 or $ jog aoc -2
Finally, you can specify an exact puzzle using the day’s number. E.g. for day 12:
$ jog aoc 12
This will create a puzzle subdirectory if it doesn’t already exist, and run the solvers within if it does.
Configuration
No configuration is necessary, but the following settings are available. They can be configured using any compatible config file recognised by jogger.
year: The year of the Advent of Code challenge being attempted. Controls verifying puzzle availability and downloading puzzle input data. This setting is optional (it defaults to the current year) but recommended. Without it, you can’t re-run solutions from previous years, or continue to work on puzzles after December 31.
session_cookie: The value of your cookie for an authenticated session at https://adventofcode.com. Used to download puzzle input, which is unique per user. If not provided, puzzle input cannot be downloaded and must be populated manually. It is strongly recommended to put this setting in joggerenv.cfg and ignore/exclude that file from any version control in use.
Input parsing
A solvers.py file can optionally contain an input_parser attribute, which should reference a function that takes a single argument (the raw input data) and returns a value to be passed to the solvers. This can be used to parse the input data into a more convenient format for the solvers to work with. If not present, the raw input data will be passed to the solvers as-is.
Specifying an input_parser is useful when the input needs to be processed in the same way for both parts of the puzzle. If only one part requires certain processing, that logic can and should reside in the solver itself.
The solvers.py template created when initialising a new puzzle contains a default value for input_parser that splits the input data into lines, as this is a common format for puzzle input. This can be removed or altered if need be.
A handful of parsers for common puzzle input formatters are provided in aoc.utils.parsing. These can be provided as values for input_parser. Available options are:
split_lines: Splits the input data into lines and returns a list of strings.
int_lines: Splits the input data into lines and returns a list of integers.
split_commas: Splits the input data into comma-separated values and returns a list of strings.
int_commas: Splits the input data into comma-separated values and returns a list of integers.
It is also possible to provide a custom parser function:
# solvers.py
def input_parser(input_data):
# Calculate the sum of each "group" of line-separated integers.
# Each group is separated by two line breaks.
return [sum(map(int, input_item.split('\n'))) for group in input_data.split('\n\n')]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file task-jogger-aoc-0.1.1.tar.gz
.
File metadata
- Download URL: task-jogger-aoc-0.1.1.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/6.8.0 pkginfo/1.9.6 requests/2.31.0 requests-toolbelt/1.0.0 tqdm/4.66.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52ecf4c34dd3b047d2565bda4adc3225b45e44093d8c2cc9d4596d912571e198 |
|
MD5 | 3e0f6f2b5c6166f90c67f810ec666e56 |
|
BLAKE2b-256 | 4e82e1d184bad0d73279fec1e432b3826cd2f12608e19bce313476258e159f1d |
File details
Details for the file task_jogger_aoc-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: task_jogger_aoc-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/6.8.0 pkginfo/1.9.6 requests/2.31.0 requests-toolbelt/1.0.0 tqdm/4.66.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63ffe8eda0da0906226406ef055c940757445156c1777654928b621df5f67675 |
|
MD5 | 800abb9f1942e1102a1b27ef544ae4b1 |
|
BLAKE2b-256 | 73d561d3cecb0b641bb95229d8e6ee376fed01a8fbef52fcdbf5d8f76f0395f9 |