Expand description

We map each pair of elements to its count, then update the counts according to the replacement rules, allowing us to compute each step in O(rules) time.

To avoid using a set for our common accesses, we use a variant of perfect hashing to let us track pairs via an array.

Structs

Polymers contain a template and rules, and we also track the set of hashes to allow us to reverse our pairs (including in rules) to their original elements.

Constants

There are 100 rules.

The template is 20 elements long.

Functions

We only care about pairs of elements, and in particular only care about the pairs that we see in the pair rules. As such, we can hash everything, allowing us to use a small array and indexing rather than a set.

Directly calls polymerize.

Directly calls polymerize.

Run the polymerization process for the given amount of steps, then reverse the element identifiers to count the individual characters.

Type Definitions

We represent pairs via hashes, which we’ll operate a lot on.

Rules map one pair to two new pairs via insertion.