Day 10: Adapter Array

Classes:

HistogramOfDeltas(mapping)

Represent a histogram of differences between adapters in jolts.

Functions:

histogram_differences(adapters)

Compute the histogram of jolt differences in adapters.

compute_result(histo)

Analyze the histogram of jolt differences.

parse(lines)

Parse the specification of the adapters given as lines.

class HistogramOfDeltas(mapping: Mapping[int, int])[source]

Represent a histogram of differences between adapters in jolts.

Methods:

__new__(cls, mapping)

Enforce histogram constraints.

__getitem__(key)

Retrieve the count corresponding to the histogram bin given with key.

__iter__()

Iterate over histogram bins.

__len__()

Retrieve the number of the histogram bins.

static __new__(cls, mapping: Mapping[int, int]) HistogramOfDeltas[source]

Enforce histogram constraints.

Requires
  • all(count >= 0 for count in mapping.values())

  • all(delta > 0 for delta in mapping.keys())

__getitem__(key: int) int[source]

Retrieve the count corresponding to the histogram bin given with key.

__iter__() Iterator[int][source]

Iterate over histogram bins.

__len__() int[source]

Retrieve the number of the histogram bins.

histogram_differences(adapters: List[int]) HistogramOfDeltas[source]

Compute the histogram of jolt differences in adapters.

Requires
  • len(adapters) > 0

  • 0 not in adapters

    (The charging input not in adapters)

  • all(adapter >= 0 for adapter in adapters)

  • len(set(adapters)) == len(adapters)

Ensures
  • len(adapters) == 0sum(result.values()) == 0

    (Empty histogram on empty input)

  • sum(result.values()) == len(adapters) + 1

compute_result(histo: HistogramOfDeltas) int[source]

Analyze the histogram of jolt differences.

Returns

the product of the respective counts of 1s and 3s differences

Ensures
  • result >= 0

parse(lines: Lines) List[int][source]

Parse the specification of the adapters given as lines.

Returns

List of corresponding number of adapter jolts

Requires
  • all(re.match(r"^(0|[1-9][0-9]*)Z", line) for line in lines)

Ensures
  • len(lines) == len(result)