2021 Day 14

Author

Nathan Moore

— Day 14: Extended Polymerization —

We need to reinforce the submarine with some polymers.

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?

library(tidyverse)

my_file <- here::here("2021", "data-2021-14.txt")
x <- readLines(my_file)

Split the input to appropriate pieces.

pol = unlist(strsplit(x[[1]], ""))

rules = strsplit(x[3:length(x)], " -> ")

rr = list()

for (r in 1:length(rules)) {
    rr[rules[[r]][1]] = rules[[r]][2]
}

Loop through ten times.

old_list = pol
new_list = vector()

for (i in 1:10) {
    # figure out the length to loop
    loop = length(old_list) - 1
    for (j in 1:loop) {
        # allocate elements to new list
        new_list = c(new_list, old_list[j])
        # get the substitute key
        key = paste0(old_list[j], old_list[j+1])
        if (key %in% names(rr)){
            # print(rr[key])
            new_list = c(new_list, unlist(rr[key]))
        } else {
            print(key)
        }
    }
    new_list = c(new_list, old_list[j+1])
    old_list = new_list
    new_list = vector()
}

sort(table(old_list))
old_list
   O    V    F    B    S    H    C    K    P    N 
 506 1492 1522 1530 1749 2184 2366 2472 2528 3108 
3108 - 506
[1] 2602

— Part Two —

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?

# there might be a pattern to this
# because 40 doesn't sound that big
# but it is probably massively long lists? 
# or maybe I need to only keep track of how many times characters appear
# brute force to start
# 20 is too much to start

# old_list = pol
# new_list = vector()
# 
# for (i in 1:20) {
#     # figure out the length to loop
#     loop = length(old_list) - 1
#     for (j in 1:loop) {
#         # allocate elements to new list
#         new_list = c(new_list, old_list[j])
#         # get the substitute key
#         key = paste0(old_list[j], old_list[j+1])
#         if (key %in% names(rr)){
#             # print(rr[key])
#             new_list = c(new_list, unlist(rr[key]))
#         } else {
#             print(key)
#         }
#     }
#     new_list = c(new_list, old_list[j+1])
#     old_list = new_list
#     new_list = vector()
# }
# 
# sort(table(old_list))

Another approach: go through and see if there are consistent amounts of letters added per round.

for (k in 1:8) {
    old_list = pol
    new_list = vector()
    
    for (i in 1:k) {
        # figure out the length to loop
        loop = length(old_list) - 1
        for (j in 1:loop) {
            # allocate elements to new list
            new_list = c(new_list, old_list[j])
            # get the substitute key
            key = paste0(old_list[j], old_list[j+1])
            if (key %in% names(rr)){
                # print(rr[key])
                new_list = c(new_list, unlist(rr[key]))
            } else {
                print(key)
            }
        }
        new_list = c(new_list, old_list[j+1])
        old_list = new_list
        new_list = vector()
    }
    
    print(sort(table(old_list)))
}
old_list
V S F N O P H K B C 
1 2 3 3 3 3 4 5 6 9 
old_list
 O  S  V  F  P  B  N  H  K  C 
 3  4  4  6  7  9  9 10 10 15 
old_list
 O  V  S  F  K  B  C  N  P  H 
 4  7 10 13 18 20 20 20 20 21 
old_list
 O  V  S  F  B  K  C  N  H  P 
 6 15 21 24 33 36 37 43 45 45 
old_list
 O  V  F  S  B  K  C  H  N  P 
14 29 55 58 63 68 70 73 84 95 
old_list
  O   V   F   S   B   H   C   K   N   P 
 34  74  94 108 114 144 146 146 172 185 
old_list
  O   V   F   B   S   H   K   C   P   N 
 69 148 193 204 230 271 294 309 350 365 
old_list
  O   V   F   B   S   H   C   K   P   N 
133 341 385 388 440 551 602 613 666 746