-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (57 loc) · 1.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"os"
"sort"
"strings"
)
func ReadInput(f string) string {
data, _ := os.ReadFile(f)
return string(data)
}
func Solve(s *string) (total int) {
parts := strings.Split(strings.TrimSpace(*s), "\n\n")
polymer := parts[0]
pairsRules := parts[1]
m := make(map[string]string)
for _, pairRule := range strings.Split(pairsRules, "\n") {
rule := strings.Split(pairRule, " -> ")
left, right := rule[0], rule[1]
m[left] = right
}
pairsCounter := make(map[string]int)
for i := 1; i < len(polymer); i++ {
previous := i - 1
pair := polymer[previous : i+1]
pairsCounter[pair]++
}
step := 0
charCounter := make(map[string]int)
// Part 1
//for step < 10 {
for step < 40 {
updateCounter := make(map[string]int)
updateCharCounter := make(map[string]int)
for k, v := range pairsCounter {
k1, k2 := string(k[0])+m[k], m[k]+string(k[1])
updateCounter[k1] += v
updateCounter[k2] += v
updateCharCounter[string(k[0])] += v
updateCharCounter[m[k]] += v
}
pairsCounter = updateCounter
charCounter = updateCharCounter
step++
}
charCounter[string(polymer[len(polymer)-1])]++
counts := make([]int, 0)
for _, v := range charCounter {
counts = append(counts, v)
}
sort.Ints(counts)
return counts[len(counts)-1] - counts[0]
}
func main() {
problem_input := ReadInput("input.txt")
fmt.Println(Solve(&problem_input))
}