Day 10: Adapter Array¶
Classes:
|
Represent a histogram of differences between adapters in jolts. |
Functions:
|
Compute the histogram of jolt differences in |
|
Analyze the histogram of jolt differences. |
|
Parse the specification of the adapters given as |
- 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())
- 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) == 0
⇒sum(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