Skip to content

Instantly share code, notes, and snippets.

@p7g
Created December 14, 2021 16:53
Show Gist options
  • Select an option

  • Save p7g/06790583e39eabd9383e421742eed3d6 to your computer and use it in GitHub Desktop.

Select an option

Save p7g/06790583e39eabd9383e421742eed3d6 to your computer and use it in GitHub Desktop.
advent of code 2021 day 14 in golang
package main
import (
"math"
"strings"
)
const data = `
`
func main() {
ss := strings.Split(strings.Trim(data, " \n"), "\n\n")
tmpl := ss[0]
rules := make(map[[2]byte]byte)
for _, inst := range strings.Split(ss[1], "\n") {
rules[[2]byte{inst[0], inst[1]}] = inst[6]
}
sPairs := make(map[[2]byte]int)
for i := 0; i+1 < len(tmpl); i += 1 {
sPairs[[2]byte{tmpl[i], tmpl[i+1]}] += 1
}
for step := 1; ; step += 1 {
newPairs := make(map[[2]byte]int)
for pair, val := range sPairs {
if ins, ok := rules[pair]; ok {
newPairs[[2]byte{pair[0], ins}] += val
newPairs[[2]byte{ins, pair[1]}] += val
} else {
newPairs[pair] += val
}
}
sPairs = newPairs
if step == 40 {
break
}
}
letterCount := make(map[byte]int)
for pair, count := range sPairs {
letterCount[pair[0]] += count
}
letterCount[tmpl[len(tmpl)-1]] += 1
max := 0
min := math.MaxInt64
for _, count := range letterCount {
if count > max {
max = count
}
if count < min {
min = count
}
}
println(max - min)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment