-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
129 lines (107 loc) · 2.47 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
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"io"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader, steps int) int {
groups := aoc.StringGroups(aoc.ReaderToStrings(input))
template := groups[0][0]
transformations := make(map[string]rune)
for _, line := range groups[1] {
del := aoc.NewDelimiter(line, " -> ")
transformations[del.GetString(0)] = rune(del.GetString(1)[0])
}
head := &Node{
r: rune(template[0]),
}
cur := head
for i := 1; i < len(template); i++ {
n := &Node{
r: rune(template[i]),
previous: cur,
}
cur.next = n
cur = n
}
for i := 0; i < steps; i++ {
transform(head, transformations)
}
return getDifference(head)
}
func transform(cur *Node, transformations map[string]rune) {
previous := cur.r
cur = cur.next
for cur != nil {
s := fmt.Sprintf("%c%c", previous, cur.r)
res, contains := transformations[s]
if contains {
n := &Node{
r: res,
previous: cur.previous,
next: cur,
}
cur.previous.next = n
cur.previous = n
}
previous = cur.r
cur = cur.next
}
}
func getDifference(cur *Node) int {
m := make(map[rune]int)
for cur != nil {
m[cur.r]++
cur = cur.next
}
minMax := aoc.NewMinerMaxer()
for _, v := range m {
minMax.Add(v)
}
return minMax.GetMax() - minMax.GetMin()
}
type Node struct {
r rune
previous *Node
next *Node
}
func fs2(input io.Reader, steps int) int {
groups := aoc.StringGroups(aoc.ReaderToStrings(input))
template := groups[0][0]
transformations := make(map[string]rune)
for _, line := range groups[1] {
del := aoc.NewDelimiter(line, " -> ")
transformations[del.GetString(0)] = rune(del.GetString(1)[0])
}
pairs := make(map[string]int)
for i := 1; i < len(template); i++ {
pairs[fmt.Sprintf("%c%c", template[i-1], template[i])]++
}
for i := 0; i < steps; i++ {
pairs = transformPairs(pairs, transformations)
}
m := make(map[rune]int)
for s, v := range pairs {
m[rune(s[0])] += v
m[rune(s[1])] += v
}
minMax := aoc.NewMinerMaxer()
for _, v := range m {
minMax.Add(v)
}
return (minMax.GetMax()-minMax.GetMin())/2 + 1
}
func transformPairs(pairs map[string]int, transformations map[string]rune) map[string]int {
res := make(map[string]int, len(pairs))
for k, count := range pairs {
if dest, exists := transformations[k]; exists {
pair1 := fmt.Sprintf("%c%c", rune(k[0]), dest)
pair2 := fmt.Sprintf("%c%c", dest, rune(k[1]))
res[pair1] += count
res[pair2] += count
} else {
res[k] = count
}
}
return res
}