Giter VIP home page Giter VIP logo

advent-of-code-helpers's Introduction

Advent of Code helpers

image

image

image

image

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()

advent-of-code-helpers's People

Contributors

mvaal avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.