Skip to main content

Advent of Code helper functions

Project description

https://img.shields.io/pypi/pyversions/advent-of-code-helpers.svg https://badge.fury.io/py/advent-of-code-helpers.svg https://github.com/mvaal/advent-of-code-helpers/workflows/build/badge.svg 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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for advent-of-code-helpers, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size advent_of_code_helpers-0.1.1-py3.8.egg (3.9 kB) File type Egg Python version 3.8 Upload date Hashes View
Filename, size advent_of_code_helpers-0.1.1-py3-none-any.whl (4.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size advent-of-code-helpers-0.1.1.tar.gz (8.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page