Day 17: Conway Cubes

Classes:

Point(x, y, z)

Represent a cube in 3D.

Activity(active_cubes)

Represent the current active cubes in the energy source

Functions:

are_neighbours(point, another)

Check whether the point and another are adjacent cubes.

list_neighbourhood(point)

List all neighbouring cubes w.r.t.

apply(activity)

Perform a single cycle of the initialization starting from activity.

parse_initial(lines)

Parse lines into the state of the energy source.

repr_activity(activity)

Represent the activity as a string for easier testing/debugging.

count_active(activity)

Count number of active cells in the activity.

class Point(x: int, y: int, z: int)[source]

Represent a cube in 3D.

Methods:

__new__(cls, x, y, z)

Create the point as tuple with the given coordinates.

__getitem__(index)

Get the item at the given integer index.

static __new__(cls, x: int, y: int, z: int) Point[source]

Create the point as tuple with the given coordinates.

__getitem__(index: int) int[source]

Get the item at the given integer index.

class Activity(active_cubes: Set[Point])[source]

Represent the current active cubes in the energy source

Methods:

__new__(cls, active_cubes)

Create an activity as a set of active cubes.

__iter__()

Iterate over the lines.

__contains__(item)

Return True if the cube is activated.

__len__()

Return the number of active cubes.

static __new__(cls, active_cubes: Set[Point]) Activity[source]

Create an activity as a set of active cubes.

__iter__() Iterator[Point][source]

Iterate over the lines.

__contains__(item: Point) bool[source]

Return True if the cube is activated.

__len__() int[source]

Return the number of active cubes.

are_neighbours(point: Point, another: Point) bool[source]

Check whether the point and another are adjacent cubes.

list_neighbourhood(point: Point) List[Point][source]

List all neighbouring cubes w.r.t. the point cube.

Ensures
  • all(
        are_neighbours(point=point, another=neighbour)
        for neighbour in result
    )
    
  • point not in result

  • len(result) == 26

apply(activity: Activity) Activity[source]

Perform a single cycle of the initialization starting from activity.

Returns

the new state of the energy source

parse_initial(lines: Lines) Activity[source]

Parse lines into the state of the energy source.

Requires
  • all(re.match(r'^[.#]+Z', line) for line in lines)

  • len(lines) >= 1
    and all(
        len(line) == len(lines[0])
        for line in lines
    )
    
Ensures
  • sum(line.count('#') for line in lines) == len(result)

repr_activity(activity: Activity) str[source]

Represent the activity as a string for easier testing/debugging.

count_active(activity: Activity) int[source]

Count number of active cells in the activity.

Ensures
  • result >= 0