Advent of Code 2021 in Julia – Day 14: Extended Polymerization

Table of Contents

The puzzle today is akin to that in day 6: A naive, recursive implementation works for part 1, but is too intensive for part 2, and requires a different approach.

Part 1

Show challenge - day 14, part 1

The incredible pressures at this depth are starting to put a strain on your submarine. The submarine has polymerization equipment that would produce suitable materials to reinforce the submarine, and the nearby volcanically-active caves should even have the necessary input elements in sufficient quantities.

The submarine manual contains instructions for finding the optimal polymer formula; specifically, it offers a polymer template and a list of pair insertion rules (your puzzle input). You just need to work out what polymer would result after repeating the pair insertion process a few times.

For example:

NNCB

CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C

The first line is the polymer template - this is the starting point of the process.

The following section defines the pair insertion rules. A rule like AB -> C means that when elements A and B are immediately adjacent, element C should be inserted between them. These insertions all happen simultaneously.

So, starting with the polymer template NNCB, the first step simultaneously considers all three pairs:

  • The first pair (NN) matches the rule NN -> C, so element C is inserted between the first N and the second N.
  • The second pair (NC) matches the rule NC -> B, so element B is inserted between the N and the C.
  • The third pair (CB) matches the rule CB -> H, so element H is inserted between the C and the B.

Note that these pairs overlap: the second element of one pair is the first element of the next pair. Also, because all pairs are considered simultaneously, inserted elements are not considered to be part of a pair until the next step.

After the first step of this process, the polymer becomes NCNBCHB.

Here are the results of a few steps using the above rules:

Template:     NNCB
After step 1: NCNBCHB
After step 2: NBCCNBBBCBHCB
After step 3: NBBBCNCCNBBNBNBBCHBHHBCHB
After step 4: NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB

This polymer grows quickly. After step 5, it has length 97; After step 10, it has length 3073. After step 10, B occurs 1749 times, C occurs 298 times, H occurs 161 times, and N occurs 865 times; taking the quantity of the most common element (B, 1749) and subtracting the quantity of the least common element (H, 161) produces 1749 - 161 = 1588.

Apply 10 steps of pair insertion to the polymer template and find the most and least common elements in the result. What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?

Here’s the provided input. It’s quite long so don’t try to scroll through:

Show input - day 14
OOFNFCBHCKBBVNHBNVCP

PH -> V
OK -> S
KK -> O
BV -> K
CV -> S
SV -> C
CK -> O
PC -> F
SC -> O
KC -> S
KF -> N
SN -> C
SF -> P
OS -> O
OP -> N
FS -> P
FV -> N
CP -> S
VS -> P
PB -> P
HP -> P
PK -> S
FC -> F
SB -> K
NC -> V
PP -> B
PN -> N
VN -> C
NV -> O
OV -> O
BS -> K
FP -> V
NK -> K
PO -> B
HF -> H
VK -> S
ON -> C
KH -> F
HO -> P
OO -> H
BC -> V
CS -> O
OC -> B
VB -> N
OF -> P
FK -> H
OH -> H
CF -> K
CC -> V
BK -> O
BH -> F
VV -> N
KS -> V
FO -> F
SH -> F
OB -> O
VH -> F
HH -> P
PF -> C
NF -> V
VP -> S
CN -> V
SK -> O
FB -> S
FN -> S
BF -> H
FF -> V
CB -> P
NN -> O
VC -> F
HK -> F
BO -> H
KO -> C
CH -> N
KP -> C
HS -> P
NP -> O
NS -> V
NB -> H
HN -> O
BP -> C
VF -> S
KN -> P
HC -> C
PS -> K
BB -> O
NO -> N
NH -> F
BN -> F
KV -> V
SS -> K
CO -> H
KB -> P
FH -> C
SP -> C
SO -> V
PV -> S
VO -> O
HV -> N
HB -> V

Here’s the solution to part 1:

template, pairs = split(strip(input), "\n\n") .|> string
pairs = split(pairs, "\n") .|> string
pairs = [pair[1:2] => pair[1]*pair[end]*pair[2] for pair in pairs]

for _ in 1:10
    fragments = [template[i]*template[i+1] for i in 1:(length(template)-1)]
    fragments = replace.(fragments, pairs...)
    template = *([f[begin:end-1] for f in fragments[1:end-1]]..., fragments[end])
end

tally = [count(==(c), template) => c for c in unique(template)] |> sort
tally[end].first - tally[begin].first |> print

Part 2

Show challenge - day 14, part 2

The resulting polymer isn’t nearly strong enough to reinforce the submarine. You’ll need to run more steps of the pair insertion process; a total of 40 steps should do it.

In the above example, the most common element is B (occurring 2192039569602 times) and the least common element is H (occurring 3849876073 times); subtracting these produces 2188189693529.

Apply 40 steps of pair insertion to the polymer template and find the most and least common elements in the result. What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?

Here’s the solution to part 2:

function insert_and_count_frag(frags, pairs)
    out = []
    for (k, v) in frags
        if k  keys(pairs)
            out = [out; [p => v for p in pairs[k]] |> Dict]
        else
            out = [out; frag]
        end
    end
    return merge(+, out...)
end

template, pairs = split(strip(input), "\n\n") .|> string
pairs = split(pairs, "\n") .|> string
pairs = [pair[1:2] => (pair[1]*pair[end], pair[end]*pair[2]) for pair in pairs] |> Dict
fragments = [template[i]*template[i+1] for i in 1:(length(template)-1)]
frags_count = [f => count(==(f), fragments) for f in unique(fragments)] |> Dict

for _ in 1:40
    frags_count = insert_and_count_frag(frags_count, pairs)
end

char_count = [c => 0 for c in collect("ABCDEFGHIJKLMNOPQRSTWUVXYZ")] |> Dict
char_count[template[end]] += 1
for (k, v) in frags_count
    char_count[k[1]] += v
end

counts = filter(>(0), [values(char_count)...])

maximum(counts) - minimum(counts) |> print