Day 14
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Tue, 14 Dec 2021 19:09:52 +0000 (19:09 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Tue, 14 Dec 2021 19:09:52 +0000 (19:09 +0000)
day14/example.txt [new file with mode: 0644]
day14/input.txt [new file with mode: 0644]
day14/notes.txt [new file with mode: 0644]
day14/poly-different.sh [new file with mode: 0755]
day14/poly.sh [new file with mode: 0755]
day14/task.txt [new file with mode: 0644]

diff --git a/day14/example.txt b/day14/example.txt
new file mode 100644 (file)
index 0000000..b5594dd
--- /dev/null
@@ -0,0 +1,18 @@
+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
diff --git a/day14/input.txt b/day14/input.txt
new file mode 100644 (file)
index 0000000..ef45886
--- /dev/null
@@ -0,0 +1,102 @@
+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
diff --git a/day14/notes.txt b/day14/notes.txt
new file mode 100644 (file)
index 0000000..e9c7383
--- /dev/null
@@ -0,0 +1,7 @@
+Original poly.sh is a bit brute forcey
+
+poly-different.sh was required to do part 2 without running out of memory, and
+in a reasonable amount of time. As the parts are in pairs, but the pairs
+overlap, the counting mech will get the count doubled by default, and we also
+have to remember to add 1 count of the char at the start and the end of the
+template.
diff --git a/day14/poly-different.sh b/day14/poly-different.sh
new file mode 100755 (executable)
index 0000000..b739b3b
--- /dev/null
@@ -0,0 +1,100 @@
+#!/bin/bash
+
+set -u
+
+filename="${1:-example.txt}"
+exec 3<"$filename"
+
+read -u 3 template
+read -u 3 blank
+
+declare -A inserts=()
+
+while read -u 3 poly; do
+    left="${poly% -> *}"
+    right="${poly#* -> }"
+    inserts[$left]="$right"
+done
+
+declare -A pair_result=()
+for k in "${!inserts[@]}"; do
+    char="${inserts[$k]}"
+    pair_result[$k]="${k:0:1}${char} ${char}${k:1:1}"
+done
+
+declare -A pairs=()
+for (( a=0; a<$((${#template} - 1)); a++ )); do
+    lookup="${template:$a:2}"
+    if [ "${pairs[$lookup]+abc}" ]; then
+        ((pairs[$lookup]+=1))
+    else
+        pairs[$lookup]=1
+    fi
+done
+
+do_next_step() {
+    declare -A new_pairs=()
+    for pair in "${!pairs[@]}"; do
+        count=${pairs[$pair]}
+        for new_pair in ${pair_result[$pair]}; do
+            if [ "${new_pairs[$new_pair]+abc}" ]; then
+                ((new_pairs[$new_pair]+=$count))
+            else
+                new_pairs[$new_pair]=$count
+            fi
+        done
+    done
+    unset pairs
+    declare -g -A pairs=()
+    for k in "${!new_pairs[@]}"; do
+        pairs[$k]=${new_pairs[$k]}
+    done
+}
+
+get_diff() {
+    declare -A char_counts=()
+    for k in "${!pairs[@]}"; do
+        for (( a=0; a<2; a++ )); do
+            char=${k:$a:1}
+            if [ "${char_counts[$char]+abc}" ]; then
+                ((char_counts[$char]+=${pairs[$k]}))
+            else
+                char_counts[$char]=${pairs[$k]}
+            fi
+        done
+    done
+
+    for char in ${template:0:1} ${template: -1:1}; do # start and end of template get lost from the original count
+        if [ "${char_counts[$char]+abc}" ]; then
+            ((char_counts[$char]+=1))
+        else
+            char_counts[$char]=1
+        fi
+    done
+
+    declare -a hl=()
+    for v in "${char_counts[@]}"; do
+        hl[$v]=1
+    done
+
+    declare -a vals=("${!hl[@]}")
+    h=${vals[-1]}
+    l=${vals[0]}
+
+    diff=$((h - $l))
+    diff=$((diff / 2)) # because the parts are in pairs, our counts are going to be double what they should be
+
+    echo $diff
+}
+
+for (( a=1; a<=10; a++ )); do
+    do_next_step
+done
+
+echo "Part 1: $(get_diff)"
+
+for (( a=11; a<=40; a++ )); do
+    do_next_step
+done
+
+echo "Part 2: $(get_diff)"
diff --git a/day14/poly.sh b/day14/poly.sh
new file mode 100755 (executable)
index 0000000..b617a80
--- /dev/null
@@ -0,0 +1,84 @@
+#!/bin/bash
+
+set -u
+
+filename="${1:-example.txt}"
+exec 3<"$filename"
+
+read -u 3 template
+
+read -u 3 blank
+
+declare -A inserts=()
+
+declare -a results=($template)
+
+while read -u 3 poly; do
+    left="${poly% -> *}"
+    right="${poly#* -> }"
+    inserts[$left]="$right"
+done
+
+for (( a=1; a<=10; a++ )); do
+    cur_string="${results[$(($a-1))]}"
+    for (( b=0; b<$((${#cur_string} - 1)); b++ )); do
+        lookup="${cur_string:$b:2}"
+        if [ $b -eq 0 ]; then
+            results[$a]="${lookup:0:1}${inserts[$lookup]}"
+        else
+            results[$a]+="${lookup:0:1}${inserts[$lookup]}"
+        fi
+    done
+    results[$a]+="${cur_string: -1:1}"
+done
+
+declare -A counts
+
+do_counts() {
+    local string="$1"
+    counts=()
+
+    for (( a=0; a<${#string}; a++ )); do
+        char=${string:$a:1}
+        if [ "${counts[$char]+abc}" ]; then
+            ((counts[$char]+=1))
+        else
+            counts[$char]=1
+        fi
+    done
+}
+
+get_highest_lowest() {
+    local -n __results="$1"
+    lowest_count=-1
+    lowest_letter=""
+    highest_count=-1
+    highest_letter=""
+    for key in "${!counts[@]}"; do
+        if [ $lowest_count -eq -1 ]; then
+            lowest_count=${counts[$key]}
+            lowest_letter=$key
+        elif [ ${counts[$key]} -lt $lowest_count ]; then
+            lowest_count=${counts[$key]}
+            lowest_letter=$key
+        fi
+        if [ $highest_count -eq -1 ]; then
+            highest_count=${counts[$key]}
+            highest_letter=$key
+        elif [ ${counts[$key]} -gt $highest_count ]; then
+            highest_count=${counts[$key]}
+            highest_letter=$key
+        fi
+    done
+
+    __results[$lowest_count]=$lowest_letter
+    __results[$highest_count]=$highest_letter
+}
+
+do_counts "${results[-1]}"
+declare -a result_counts=()
+get_highest_lowest result_counts
+
+declare -a temp=( "${!result_counts[@]}" )
+echo -n "Part 1: Difference between highest and lowest count: "
+echo $(("${temp[1]}" - "${temp[0]}"))
diff --git a/day14/task.txt b/day14/task.txt
new file mode 100644 (file)
index 0000000..ec44b60
--- /dev/null
@@ -0,0 +1,67 @@
+--- Day 14: Extended Polymerization ---
+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?
+
+Your puzzle answer was 2899.
+
+--- Part Two ---
+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?
+
+Your puzzle answer was 3528317079545.
+
+Both parts of this puzzle are complete! They provide two gold stars: **
+
+At this point, you should return to your Advent calendar and try another puzzle.
+
+If you still want to see it, you can get your puzzle input.
+
+You can also [Share] this puzzle.