advent-of-code-helpers

Advent of Code helper functions


Keywords
python, advent, of, code, aoc
License
MIT
Install
pip install advent-of-code-helpers==0.1.1

Documentation

Advent of Code helpers

Test Coverage Maintainability

Advent of Code helper functions

from aoc.helpers import output, read_input_from_file, input_lines
from aoc import template

Setup Guide

Install with pip

pip install advent-of-code-helpers

Helper Usage

read_input_from_file reads the data as a single line from a file

from aoc.helpers import read_input_from_file
read_input_from_file('path/to/input_data')

input_lines returns a list of strings from the string input

from aoc.helpers import input_lines
input_lines('single\nstring\ninput')

output prints the result to console and writes to an output file if an output directory is provided

from aoc.helpers import output
output('result', part(int), day(int), year(int), output_dir(str), file_prefix(str))

Template Usage

You can specify data from a file using the data(input) function.

You can specify an output directory for output using the output(output) function. If left empty, it will still print to screen, but will not write the result to a file. If given an output directory, the results will be appended to the file so you can easily go back and look at previous results.

Examples

from aoc import template


class Part1(template.Part1):
    def solve(self):
        # Read input
        lines = input_lines(self.input())
        # Do some work here

        # Sample output
        result = ','.join(lines)
        return result


def main():
    output_dir = '../out'
    test_data = os.path.join(os.path.dirname(__file__),
                             'resources/test_input.txt')
    Part1(1, 2018).data(test_data).output(output_dir)

    data = os.path.join(os.path.dirname(__file__), 'resources/input.txt')
    Part1(1, 2018).data(data).output(output_dir)


if __name__ == "__main__":
    main()

More usage in the example.

Template Usage with Other Libraries

If you want to use your own input reader or a library like advent-of-code-data, you can override the input method.

Examples

from aoc import template
from aoc.helpers import input_lines
from aocd import get_data


class Part1(template.Part1):
    def input(self):
        if self.input_file:
            return super().input()
        else:
            return get_data(day=self.day, year=self.year)

    def solve(self):
        # Read input
        lines = input_lines(self.input())
        # Do some work here

        # Sample output
        result = ','.join(lines)
        return result


def main():
    Part1(1, 2018).output('../out')


if __name__ == "__main__":
    main()