/advent-of-code-2023

Advent of Code 2023 solutions repository

Primary LanguagePythonMIT LicenseMIT

⭐ Advent of Code 2023 - Python ⭐

GitHub GitHub Workflow Status

Source Code: https://github.com/nekeal/advent-of-code-2023


Solutions for Advent of Code 2023 in Python

Use this project as a template

In the master branch I will keep the template with sample day 0 solution. Therefore, I strongly recommend to use this branch as a template for your own solutions by forking this repository or working with a copy of it.

Usage

This template comes with a CLI tool powered by Typer to help you with boilerplate code and basic tasks like creating a directory for a new day, running tests, etc.

Starting solution for a new day

aoc new-day <day>

It will create a src/aoc/day_<day> directory with a __init__.py and a test_solution.py python files. It will also create text files for both test and real data in the data/ directory.

Implementing a solution for a day

BaseChallenge class is provided to help you with the boilerplate code. By default, each day's solution inherits from this class. You need to implement the part1 and part2 methods, and they should return the correct answer for each part.

Running solution and checking the answer

This will usually be used for debugging purposes.

$ aoc run 0
Using data from 00_input.txt
Day 0 - Part 1: 1
Day 0 - Part 2: 55

This command will run the solution for the given day using data/00_input.txt file and print the answers for both parts.

If you also want to see the result for the test data, you can use the -t/--test-data flag.

$ aoc run <day> -t
Using data from 00_test_input.txt
Day 0 - Part 1: 1
Day 0 - Part 2: 10

Using data from 00_input.txt
Day 0 - Part 1: 1
Day 0 - Part 2: 55

Verifying solution

You can verify your solution by running pytest tests. There is a generic test case for each day that checks both parts of a solution against the correct answer. To use it you need to configure correct answers on the test class for a given day.

class TestChallenge(BaseTestChallenge):
    challenge_class = Challenge
    expected_results_from_test_data = ("Expected result for part 1", "Expected result for part 2")
    expected_results_from_real_data = ("Expected result for part 1", "Expected result for part 2")

Then you can run the tests for a given day with:

aoc verify <day>

To test only part 1 or part 2, you can use the -1/-2 flag.

aoc verify <day> -1

To test only for sample data, you can use the -t/--test-data-only flag.

aoc verify <day> -t

To check for a regression (😉) you can run all tests at once:

aoc verify

Development

  • Clone this repository
  • Requirements:
  • Create a virtual environment and install dependencies
poetry install
  • Activate the virtual environment
poetry shell

Testing

aoc verify

Pre-commit

Pre-commit hooks run all the auto-formatters (e.g. ruff), linters (e.g. mypy), and other quality checks to make sure the changeset is in good shape before a commit/push happens.

You can install hooks with (runs for each commit):

pre-commit install

Or if you want to run all checks manually for all files:

pre-commit run --all-files

This project was generated using the cookiecutter-python-package template.