2021 Day 14 Complete!

This commit is contained in:
Brian Buller 2021-12-14 07:51:11 -06:00
parent 8d22b9a32a
commit dc8dbfea6b
4 changed files with 331 additions and 0 deletions

102
2021/day14/input Normal file
View File

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

108
2021/day14/main.go Normal file
View File

@ -0,0 +1,108 @@
package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
var template string
var insertions map[string]string
func main() {
insertions = make(map[string]string)
inp := h.StdinToStringSlice()
template = inp[0]
for i := 2; i < len(inp); i++ {
pts := strings.Split(inp[i], " -> ")
insertions[pts[0]] = pts[1]
}
part1(template)
fmt.Println()
part2(template)
}
func transform(inp string) string {
var ret string
for i := 0; i < len(inp)-1; i++ {
pair := inp[i : i+2]
ret = ret + string(inp[i]) + insertions[pair]
}
ret = ret + string(inp[len(inp)-1])
return ret
}
func part1(inp string) {
for i := 0; i < 10; i++ {
inp = transform(inp)
}
countMap := make(map[byte]int)
for i := range inp {
countMap[inp[i]]++
}
var mostCommon, leastCommon byte
mostCount := h.MIN_INT
leastCount := h.MAX_INT
for k, v := range countMap {
if v > mostCount {
mostCount = v
mostCommon = k
}
if v < leastCount {
leastCount = v
leastCommon = k
}
}
fmt.Println("# Part 1")
fmt.Printf("Most Common: %s (%d)\n", string(mostCommon), mostCount)
fmt.Printf("Least Common: %s (%d)\n", string(leastCommon), leastCount)
fmt.Println("Answer", (mostCount - leastCount))
}
func part2Transform(pairs map[string]int) map[string]int {
ret := make(map[string]int)
for k, v := range pairs {
add := insertions[k]
p1, p2 := string(k[0])+add, add+string(k[1])
ret[p1] += v
ret[p2] += v
}
return ret
}
func part2(inp string) {
// So the obvious way doesn't work for this many steps.
// Let's try just dealing with pairs.
pairs := make(map[string]int)
for i := 0; i < len(inp)-1; i++ {
pair := inp[i : i+2]
pairs[pair]++
}
for i := 0; i < 40; i++ {
pairs = part2Transform(pairs)
}
countMap := make(map[byte]int)
for k, v := range pairs {
countMap[k[0]] = countMap[k[0]] + v
}
countMap[inp[len(inp)-1]]++
var mostCommon, leastCommon byte
mostCount := h.MIN_INT
leastCount := h.MAX_INT
for k, v := range countMap {
if v > mostCount {
mostCount = v
mostCommon = k
}
if v < leastCount {
leastCount = v
leastCommon = k
}
}
fmt.Println("# Part 2")
fmt.Printf("Most Common: %s (%d)\n", string(mostCommon), mostCount)
fmt.Printf("Least Common: %s (%d)\n", string(leastCommon), leastCount)
fmt.Println("Answer", (mostCount - leastCount))
}

103
2021/day14/problem Normal file
View File

@ -0,0 +1,103 @@
# Advent of Code
--- 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 2621.
--- 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 2843834241366.
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.
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2021/about
. https://adventofcode.com/2021/events
. https://adventofcode.com/2021/settings
. https://adventofcode.com/2021/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2021/support
. https://adventofcode.com/2021
. https://adventofcode.com/2021
. https://adventofcode.com/2021/support
. https://adventofcode.com/2021/sponsors
. https://adventofcode.com/2021/leaderboard
. https://adventofcode.com/2021/stats
. https://adventofcode.com/2021/sponsors
. https://en.wikipedia.org/wiki/Polymerization
. https://adventofcode.com/2021
. https://adventofcode.com/2021/day/14/input

18
2021/day14/testinput Normal file
View File

@ -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