-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDayTen.go
161 lines (123 loc) · 3.4 KB
/
DayTen.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package adventofcode2015
import (
"fmt"
"strconv"
"strings"
)
func lookAndSay(input string) string {
currentChar := ""
currentCount := 0
output := ""
for j := 0; j < len(input); j++ {
checkChar := input[j : j+1]
if checkChar == currentChar {
//Same char increase count
currentCount++
} else {
//different char write previous and change
if currentCount > 0 {
output += strconv.Itoa(currentCount)
output += currentChar
}
currentChar = checkChar
currentCount = 1
}
}
output += strconv.Itoa(currentCount)
output += currentChar
return output
}
func DayTenExample() {
fmt.Println("Day 10 - Example")
input := "1"
for i := 0; i < 5; i++ {
input = lookAndSay(input)
}
fmt.Println("Output:", input)
}
func DayTenPartOne() {
fmt.Println("Day 10 - Part One")
input := "1113122113"
for i := 0; i < 40; i++ {
input = lookAndSay(input)
}
fmt.Println("Output:", len(input))
}
type element struct {
number int
name string
pattern string
nextStage []string
}
func DayTenPartTwo() {
fmt.Println("Day 10 - Part Two")
input := "1113122113"
fmt.Println(input)
elementFile := strings.Split(ReadFile("day10-elements.txt"), "\n")
elementList := make([]element, len(elementFile)+1)
for i := 0; i < len(elementFile); i++ {
elementParts := strings.Split(elementFile[i], "\t")
elementNumber, _ := strconv.Atoi(elementParts[1])
elementName := elementParts[2]
elementPattern := elementParts[3]
var nextStage []string
elementList[elementNumber] = element{elementNumber, elementName, elementPattern, nextStage}
}
//for each element
// look at next pattern
// if simple next is next elment
// else next is a list of elements including the next one
elementNameMap := buildNextStateMap(elementList)
var output []element
output = append(output, elementNameMap["Fr"])
for x := 0; x < 50; x++ {
output = getNextState(output, elementNameMap)
}
finalPatternLength := 0
for y := 0; y < len(output); y++ {
finalPatternLength += len(output[y].pattern)
}
fmt.Println(finalPatternLength)
}
func getNextState(input []element, elementMap map[string]element) []element {
output := make([]element, 0)
for i := 0; i < len(input); i++ {
inputElement := input[i]
for j := 0; j < len(inputElement.nextStage); j++ {
nextElement := elementMap[inputElement.nextStage[j]]
output = append(output, nextElement)
}
}
return output
}
func buildNextStateMap(elementList []element) map[string]element {
elementNameMap := make(map[string]element)
elementNames := make(map[string]element)
for _, value := range elementList {
elementNames[value.name] = value
}
for i := 1; i < len(elementList); i++ {
currentElement := elementList[i]
//fix pattern
newPattern := ""
var nextStageArray []string
oldPatternGroups := strings.Split(currentElement.pattern, ".")
for j := 0; j < len(oldPatternGroups); j++ {
if _, exists := elementNames[oldPatternGroups[j]]; !exists {
newPattern = oldPatternGroups[j]
nextStageArray = append(nextStageArray, currentElement.name)
} else {
nextStageArray = append(nextStageArray, oldPatternGroups[j])
}
}
if i+1 < len(elementList) {
elementList[i+1].nextStage = nextStageArray
}
currentElement.pattern = newPattern
elementNameMap[currentElement.name] = currentElement
}
firstElement := elementNameMap["H"]
firstElement.nextStage = []string{"H"}
elementNameMap["H"] = firstElement
return elementNameMap
}