DEV Community

Cover image for Introducing The Advent of Code Cookiecutter
Lachlan Eagling
Lachlan Eagling

Posted on • Originally published at on

Introducing The Advent of Code Cookiecutter

Well, it's that time of year again with the fresh 2019 Advent of Code challenges landing today. In years past I have constantly found myself repeating myself with writing out function stubs, unit tests, creating files to hold challenge input and results etc.

This year I have done all the work upfront, introducing the Advent of Code Cookie-cutter a Python project to bootstrap your Advent of Code efforts. Out of the box, it provides function stubs for implementing solutions to all the challenges, empty files to simply drop your challenge inputs and answers into, and then unit tests to provide peace of mind when refactoring and improving solutions to the challenges.

Below is an example of solving the part 1 of the first challenge from the 2018 Advent of Code challenges with the Advent of Code Cookie-cutter project.

from advent_of_code.utils import load_input, save_answers

INPUT = load_input(1)

def part1():
    return sum(int(x) for x in INPUT)

def part2():
    return "Challenge not solved"

if __name__ == " __main__":
    answer1 = part1()
    answer2 = part2()
    print(f"Day 1 - Part 1 Answer: {answer1}")
    print(f"Day 1 - Part 2 Answer: {answer2}")
    save_answers(answer1, 1, 1)
    save_answers(answer2, 1, 2)

Once the output answer has been verified as correct unit tests can be executed across all of the challenges to ensure that the answer remains correct when refactoring and improving solutions.

Advent of Code Cookiecutter - Unit Test Execution

For instructions on getting up and running with this, see the Github project. Suggested improvements are encouraged and welcome too 🙏.

Discussion (0)