-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.go
183 lines (165 loc) · 4.17 KB
/
19.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"io/ioutil"
"regexp"
"strings"
)
func parseInput() []byte {
input, err := ioutil.ReadFile("input.txt")
if err != nil {
panic(err)
}
return input
}
var newLine = "\r\n"
func prepInput(input []byte) ([]string, []string) {
parts := strings.Split(strings.TrimSpace(string(input)), newLine+newLine)
rules := strings.Split(parts[0], newLine)
messages := strings.Split(parts[1], newLine)
return rules, messages
}
func makeRules(ruleStrings []string) map[string][]string {
rules := make(map[string][]string)
for _, rulestr := range ruleStrings {
rule := strings.Split(rulestr, ":")
//num, _ := strconv.Atoi(rule[0])
rules[rule[0]] = strings.Split(rule[1], "|")
}
return rules
}
func countMessages(messages []string, rule []string) int {
count := 0
messages:
for _, m := range messages {
for _, r := range rule {
if m == r {
count++
continue messages
}
}
}
return count
}
func main() {
ruleStrings, messages := prepInput(parseInput())
rules := makeRules(ruleStrings)
// fullyReplaced := make(map[string][]string)
// for len(rules) > 0 {
// for key, subrules := range rules {
// done := true
// for _, sr := range subrules {
// if strings.ContainsAny(sr, "0123456789") {
// done = false
// }
// }
// if done {
// for j, sr := range subrules {
// subrules[j] = strings.ReplaceAll(strings.ReplaceAll(sr, "\"", ""), " ", "")
// }
// fullyReplaced[key] = subrules
// delete(rules, key)
// }
// }
// if _, exists := fullyReplaced["0"]; exists {
// break
// }
// for key, subrules := range rules {
// newSr := []string{}
// for _, sr := range subrules {
// fields := strings.Fields(sr)
// amount := 1
// fooKey := ""
// found := false
// foo := []string{}
// replaceMap := make(map[string][]string)
// for _, field := range fields {
// if rep, exists := fullyReplaced[field]; exists {
// amount *= len(rep)
// replaceMap[field] = rep
// foo = rep
// fooKey = field
// found = true
// break
// }
// }
// replacedRules := make([]string, amount)
// for i := 0; i < len(replacedRules); i++ {
// if found {
// replacedRules[i] = strings.ReplaceAll(sr, fooKey, foo[i])
// } else {
// replacedRules[i] = sr
// }
// }
// //replace dem rules
// // for i := 0; i < len(replacedRules); i++ {
// // replacedRules[i] = sr
// // for key, rep := range replaceMap {
// // for j := 0; j < len(rep); j++ {
// // replacedRules[i] = strings.ReplaceAll(sr, key, rep[j])
// // }
// // }
// // }
// newSr = append(newSr, replacedRules...)
// }
// rules[key] = newSr
// }
// }
rule0 := getAllRulesFor("0", rules)
count := countMessages(messages, rule0)
fmt.Println(count)
// changes := 1
// for changes > 0 {
// changes = 0
// for key, rule := range rules {
// for _, subrule := range rule {
// rules[key] = replaceRules(rules, subrule)
// }
// }
// }
// for _, subrules := range rules {
// for i, rule := range subrules {
// subrules[i] = strings.ReplaceAll(strings.ReplaceAll(rule, "\"", ""), " ", "")
// }
// }
}
func getAllRulesFor(rule string, rules map[string][]string) []string {
res := rules[rule]
changes := true
// loop:
for changes {
changes = false
loop:
for j, sr := range res {
if strings.ContainsAny(sr, "0123456789") {
changes = true
fields := strings.Fields(sr)
var key string
var replace []string
for _, field := range fields {
if rep, exists := rules[field]; exists {
replace = rep
key = field
break
}
}
length := len(replace)
if length > 1 {
additional := make([]string, length-1)
m := regexp.MustCompile("\\b" + key + "\\b")
for k, l := 0, 1; l < len(replace); k, l = k+1, l+1 {
additional[k] = m.ReplaceAllString(sr, replace[l])
}
res = append(res, additional...)
}
m := regexp.MustCompile("\\b" + key + "\\b")
res[j] = m.ReplaceAllString(sr, replace[0])
continue loop
}
}
}
for j, sr := range res {
res[j] = strings.ReplaceAll(strings.ReplaceAll(sr, "\"", ""), " ", "")
}
return res
}