-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.go
69 lines (58 loc) · 1.45 KB
/
day5.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
package main
import (
"fmt"
"io/ioutil"
"strings"
)
func main() {
puzzle1()
puzzle2()
}
func puzzle1() {
fmt.Println(len(react(getData())))
}
func puzzle2() {
fmt.Println(len(react(killUnit(getData(), getShortestPolymer()))))
}
func react(polymer string) string {
units := [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
for {
oldPolymer := polymer
for _, x := range units {
var replacer = strings.NewReplacer(x+strings.ToUpper(x), "", strings.ToUpper(x)+x, "")
polymer = replacer.Replace(polymer)
}
if oldPolymer == polymer {
return polymer
}
}
}
func killUnit(polymer string, unit string) string {
var replacer = strings.NewReplacer(unit, "", strings.ToUpper(unit), "")
return replacer.Replace(polymer)
}
func getShortestPolymer() string {
units := [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
scores := make(map[int]string)
polymer := getData()
count := 10000000
bestLetter := ""
for _, x := range units {
evisceratedPolymer := killUnit(polymer, x)
scores[len(react(evisceratedPolymer))] = x
}
for k, v := range scores {
if k < count {
bestLetter = v
count = k
}
}
return bestLetter
}
func getData() (lines string) {
content, err := ioutil.ReadFile("input.txt")
if err != nil {
panic(err)
}
return string(content)
}