You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.5 KiB
Go
80 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
file, err := os.Open("input.txt")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
insertions := make(map[string]string)
|
|
pairs := make(map[string]uint64)
|
|
|
|
var i int
|
|
var template string
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if line == "" {
|
|
continue
|
|
}
|
|
if i == 0 {
|
|
template = line
|
|
for j := 0; j < len(template)-1; j++ {
|
|
pairs[string(template[j])+string(template[j+1])]++
|
|
}
|
|
} else {
|
|
var one, two, three rune
|
|
fmt.Sscanf(line, "%c%c -> %c", &one, &two, &three)
|
|
insertions[string(one)+string(two)] = string(three)
|
|
}
|
|
i++
|
|
}
|
|
|
|
var newPairs map[string]uint64
|
|
|
|
for j := 0; j < 40; j++ {
|
|
newPairs = make(map[string]uint64)
|
|
for pair, count := range pairs {
|
|
firstNewPair := string(pair[0]) + insertions[pair]
|
|
secondNewPair := insertions[pair] + string(pair[1])
|
|
newPairs[firstNewPair] += uint64(count)
|
|
newPairs[secondNewPair] += uint64(count)
|
|
}
|
|
pairs = newPairs
|
|
if j == 9 {
|
|
fmt.Println(count(&pairs))
|
|
}
|
|
}
|
|
fmt.Println(count(&pairs))
|
|
}
|
|
|
|
func count(pairs *map[string]uint64) uint64 {
|
|
counts := make(map[string]uint64)
|
|
for pair, count := range *pairs {
|
|
counts[string(pair[1])] += uint64(count)
|
|
}
|
|
|
|
var maxCount, minCount uint64 = 0, 0xffffffffffffffff
|
|
|
|
for _, total := range counts {
|
|
if total > maxCount {
|
|
maxCount = total
|
|
}
|
|
if total < minCount {
|
|
minCount = total
|
|
}
|
|
}
|
|
|
|
return maxCount - minCount
|
|
}
|