AOChallenge Module

The module is designed to speed up the solution of certain coding challenges. The module is inspired by Advent of Code, I have not used it for anything else, but it is probably useful for other things.

The use of the module is different from the traditional ones, my goal was to be able to create a new solution easily. The Solution base class provided by the module contains some frequently used or useful functions for debugging and only the solutions need to be added.

Each time I start from the code below.

#!/usr/bin/python3 -u

from aochallenge import *


class Solution(Solver):

    def __init__(self):
        data = load(True,',',int)

#    def part1(self):

#    def part2(self):

#    def solve_more(self):

solution = Solution()
solution.main()

To be more precise, I also use type annotation, which I have taken from here.

The part1 and part2 methods are called and the returned value is displayed by the original class. If the two parts of the challenge build on each other, you can also use solve_more (generator), in which the solutions are returned with the yield keyword, so that the computation can continue without saving previous results. Check out also the example at the end of this document.

In the constructor, it makes sense to load and, if necessary, preprocess the input data, which is well done by the load function of the Solution class. Note that this function examines the program’s input arguments and decides which input file to load (test or main) based on them.

Importing data

For each challenge there are one or more test inputs and there is your challenge one. The class expects the input files to be named appropriately to be able to load automatically, but also arbitrary file name can be specified. Default filebname is ‘input’ or ‘input.txt’ and test file variant can be appended to ‘inpit’, i.e. ‘input-t’ or ‘inpuz-t.txt’.

aoc/2022/01/
|---- aoc.py        source code
|---- input-t.txt   test input
\---- input.txt     challenge input

In this case, you can run your code with the test data as follows:

$ ./aoc-2201.py -t

And with the challenge data, simply:

$ ./aoc-2201.py

In some special cases the input is a single line of data or some other simple constructs. In this case it is unnecessary to create files for each, you can simply pass a look-up-table to the load function. E.g.

INPUT = {
    None: 'My challenge input data',
    't': 'My test 1 input data',
    't2': 'My test 2 input data',
}
...
def __init__(self):
    super().__init__()
    data = self.load(lut=INPUT)

If you need which variant the solution has been run with, you can check it with variant. It returns None if no variant has bee given and the variant id (E.g. "-t") if it has been:

def __init__(self):
    ...
    if variant() is not None:
        # test variant

Using load method

The load method is used to prepare the data for further processing. The input can come from a file or from a predefined look-up-table. If the latter is not specified, file handling is automatic (see above).

load function does not only import data, but does some preprocessing on them:

def load(self,
        splitlines: bool = False,
        splitrecords: str | None = None,
        recordtype:  list[type] | tuple[type, ...] | type | None = None,
        *,
        lut: dict[str | None, Any] | None = None,
        filename: str | None = None
        ) -> list[str | int | list]

Parameters:

Note, that if splitlines is False but splitrecords is defined, only the first row will be processed. This means that if you have a one-row data set, the return element is not a two-dimensional list with a single nested list, but a simple list of values from the first row.

Using grids

The purpose of the grid submodule is to handle 2D/3D arrays and coordinates. It provides types and functions that are frequently useful in Advent of Code challenges. Here are a few examples of how it can be used:

def blur(src: grid.Grid[int]) -> grid.Grid[int]:
    dst = grid.create_grid(src, 0)
    for coord, px in grid.iter_grid(src):
        neighbors = grid.bounded_neighbors_full(coord, (0, 0), grid.boundaries(src))
        for ncoord in neighbors:
            px += grid.get_element(src, ncoord)
        pxcnt = len(neighbors) + 1
        grid.set_element(dst, coord, px // 9)
    return dst
area: Grid2D[int] = [[]]
...
colsums = [0] * grid.width(area)
rowsums = [0] * grid.height(area)
for (x, y), v in grid.iter_grid(area):
    colsums[x] += v
    rowsums[y] += v

Types

Functions

The functions below can also be used with _2d and _3d suffixes (e.g., neighbors_2d and neighbors_3d), depending on your needs. The 3D variants correspond to the Coord3D and Grid3D[T] types. Omitting the suffix defaults to 2D usage. Note that some functions are only available in a 3D context. This is clearly indicated where applicable.

Coordinate operations:

Grid operations:

Displaying temporary results

The class contains some debugging solutions to display temporary results.

Data visualization

Sometimes it’s necessary to save an image - either to analyze the current state or simply to visualize the result. The save_image function provides this capability.

scene Grid2D[str] = [
    ["#", "#", "#", "#", "#"], 
    ["#", "S", ".", ".", "#"], 
    ["#", ".", "#", ".", "#"], 
    ["#", ".", "#", "E", "#"], 
    ["#", "#", "#", "#", "#"], 
]
colors : ColorLUT[str] = {
    ".": 0xdddddd, # light gray
    "#": 0x000900, # black
    "S": 0xff0900, # red
    "E": 0x0009ff, # blue
}
save_image("scene.png", scene, colors)

Festures:

Autoimported modules and functions

The aochallenge module also imports several commonly used standard functions and modules when used with from aochallenge import *.

Imported modules:

Imported functions:

Example

PART 1: Add up all the numbers in each row separated by commas and print the maximum of these sums.

PART 2: Find the 3 largest sums, add them up and determine the final result.

Using part1 and part2:

#!/usr/bin/python2 -u

from aochallenge import *

class Solution(Solver):
    def __init__(self):
        self.data = load(True,',',int)

    def part1(self):
        return max(sum(row) for row in self.data)

    def part2(self):
        return sum(sorted(sum(row) for row in self.data)[-4:])

solution = Solution()
solution.main()

Using solve_more:

#!/usr/bin/python2 -u

from aochallenge import *

class Solution(Solver):
    def __init__(self):
        self.data = load(True,',',int)

    def solve_more(self):
        sums = sorted(sum(row) for row in self.data)
        yield sums[-2]
        yield sum(sums[-4:])

solution = Solution()
solution.main()