-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (89 loc) · 1.96 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"sort"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "14", "input.txt"))
if err != nil {
panic(err)
}
return input
}
type rule struct {
pair string
newLeftProduct, newRightProduct string
addition string
}
func solve(r io.Reader) {
scanner := bufio.NewScanner(r)
var rules []rule
scanner.Scan()
start := scanner.Text()
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
parts := strings.Split(line, " -> ")
input := parts[0]
output := parts[1]
rules = append(rules, rule{
pair: input,
newLeftProduct: string(input[0]) + output,
newRightProduct: output + string(input[1]),
addition: output,
})
}
if scanner.Err() != nil {
panic(scanner.Err())
}
counts := make(map[string]int)
polymer := make(map[string]int)
for i := 0; i < len(start)-1; i++ {
polymer[start[i:i+2]] += 1
}
for i := 0; i < len(start); i++ {
counts[start[i:i+1]]++
}
for i := 0; i < 40; i++ {
simulate(polymer, counts, rules)
}
amnts := amounts(counts)
fmt.Println(amnts[0] - amnts[len(amnts)-1])
}
func amounts(counts map[string]int) []int {
ret := make([]int, len(counts))
i := 0
for _, v := range counts {
ret[i] = v
i++
}
sort.Slice(ret, func(i, j int) bool {
return ret[i] > ret[j]
})
return ret
}
func simulate(polymer map[string]int, counts map[string]int, rules []rule) {
toAdd := make(map[string]int)
for _, r := range rules {
if amnt := polymer[r.pair]; amnt != 0 {
delete(polymer, r.pair)
toAdd[r.newLeftProduct] += amnt
toAdd[r.newRightProduct] += amnt
counts[r.addition] += amnt
}
}
for k, v := range toAdd {
polymer[k] += v
}
}
func main() {
solve(strings.NewReader("NNCB\n\nCH -> B\nHH -> N\nCB -> H\nNH -> C\nHB -> C\nHC -> B\nHN -> C\nNN -> C\nBH -> H\nNC -> B\nNB -> B\nBN -> B\nBB -> N\nBC -> B\nCC -> N\nCN -> C"))
solve(input())
}