-
Notifications
You must be signed in to change notification settings - Fork 0
/
DayNineteen.go
126 lines (90 loc) · 2.75 KB
/
DayNineteen.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
package adventofcode2015
import (
"fmt"
"strings"
)
type replacement struct {
input string
output string
}
func performReplacement(starting string, replacements map[replacement]bool) map[string]bool {
output := make(map[string]bool)
for replacementRule, _ := range replacements {
for i := 0; i < len(starting); i++ {
endPos := i + len(replacementRule.input)
endPosInRange := endPos <= len(starting)
if endPosInRange && string(starting[i:endPos]) == replacementRule.input {
newMolecule := starting[0:i]
newMolecule += replacementRule.output
newMolecule += starting[endPos:]
output[newMolecule] = true
}
}
}
return output
}
func DayNineteenExample() {
fmt.Println("Day 19 - Example")
input := strings.Split("H => HO\nH => OH\nO => HH", "\n")
replacements := make(map[replacement]bool)
for i := 0; i < len(input); i++ {
parts := strings.Split(input[i], "=>")
source := strings.Trim(parts[0], " ")
dest := strings.Trim(parts[1], " ")
replacements[replacement{source, dest}] = true
}
starting := "HOH"
output := performReplacement(starting, replacements)
fmt.Println(output)
}
func DayNineteenPartOne() {
fmt.Println("Day 19 - Part One")
input := strings.Split(ReadFile("day19-input.txt"), "\n")
replacements := make(map[replacement]bool)
for i := 0; i < len(input)-2; i++ {
parts := strings.Split(input[i], "=>")
source := strings.Trim(parts[0], " ")
dest := strings.Trim(parts[1], " ")
replacements[replacement{source, dest}] = true
}
starting := input[len(input)-1]
output := performReplacement(starting, replacements)
fmt.Println("Number of new molecules:", len(output))
}
func DayNineteenPartTwo() {
fmt.Println("Day 19 - Part Two")
input := strings.Split(ReadFile("day19-input.txt"), "\n")
replacements := make(map[replacement]bool)
for i := 0; i < len(input)-2; i++ {
parts := strings.Split(input[i], "=>")
source := strings.Trim(parts[0], " ")
dest := strings.Trim(parts[1], " ")
replacements[replacement{source, dest}] = true
}
aim := input[len(input)-1]
starting := "e"
fmt.Println(starting)
for i := 1; i < 300; i++ {
longestReplacement := 0
currentReplace := replacement{"", ""}
for replacementValue, _ := range replacements {
if strings.Contains(aim, replacementValue.output) && longestReplacement < len(replacementValue.output) {
currentReplace = replacementValue
longestReplacement = len(replacementValue.output)
}
}
if longestReplacement > 0 {
fmt.Println("Going to use:", currentReplace)
aim = strings.Replace(aim, currentReplace.output, currentReplace.input, 1)
// fmt.Println(aim)
if aim == starting {
fmt.Println("We are done at:", i)
break
}
} else {
fmt.Println("No more replacements")
break
}
}
fmt.Println(aim)
}