-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.go
280 lines (233 loc) · 6.74 KB
/
day19.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"fmt"
"os"
"strings"
"github.com/Alex-Waring/AoC/utils"
)
type Condition struct {
attr string
greater_than string
value int
destination string
}
type Workflow struct {
conditions []Condition
final_destination string
}
type Isolated_Workflow struct {
attr string
greater_than string
value int
destination string
fail_con string
}
type Part map[string]int
type Attr struct {
lower int
upper int
}
type Part_Range struct {
values map[string]Attr
next_workflow string
}
func edit_part(values map[string]Attr, attr string, lower int, upper int) map[string]Attr {
return_map := map[string]Attr{}
for key, value := range values {
if key == attr {
return_map[attr] = Attr{
lower: lower,
upper: upper,
}
} else {
return_map[key] = value
}
}
return return_map
}
func (w Isolated_Workflow) Process(part Part_Range) []Part_Range {
if w.value == 0 {
return []Part_Range{{
values: part.values,
next_workflow: w.destination,
}}
}
return_parts := []Part_Range{}
previous_upper := part.values[w.attr].upper
previous_lower := part.values[w.attr].lower
if w.greater_than == "<" {
// If the range includes part that would be successfull
if part.values[w.attr].lower < w.value {
// include the win con
win_con := Part_Range{
values: edit_part(part.values, w.attr, previous_lower, w.value-1),
next_workflow: w.destination,
}
return_parts = append(return_parts, win_con)
// Include the fail con
fail_con := Part_Range{
values: edit_part(part.values, w.attr, w.value, previous_upper),
next_workflow: w.fail_con,
}
return_parts = append(return_parts, fail_con)
} else {
return_parts = append(return_parts, Part_Range{
values: part.values,
next_workflow: w.fail_con,
})
}
} else {
if part.values[w.attr].upper > w.value {
// include the win con
win_con := Part_Range{
values: edit_part(part.values, w.attr, w.value+1, previous_upper),
next_workflow: w.destination,
}
return_parts = append(return_parts, win_con)
// Include the fail con
fail_con := Part_Range{
values: edit_part(part.values, w.attr, previous_lower, w.value),
next_workflow: w.fail_con,
}
return_parts = append(return_parts, fail_con)
} else {
return_parts = append(return_parts, Part_Range{
values: part.values,
next_workflow: w.fail_con,
})
}
}
return return_parts
}
func (w Workflow) Process(part Part) string {
for _, con := range w.conditions {
if con.greater_than == "<" {
if part[con.attr] < con.value {
return con.destination
}
} else {
if part[con.attr] > con.value {
return con.destination
}
}
}
return w.final_destination
}
func parse_workflows2(raw_workflows []string) map[string]Isolated_Workflow {
workflows := make(map[string]Isolated_Workflow)
for _, workflow := range raw_workflows {
name := strings.Split(workflow, "{")[0]
steps := strings.Split(strings.Trim(strings.Split(workflow, "{")[1], "}"), ",")
workflows[name+"_"+fmt.Sprintf("%d", len(steps)-1)] = Isolated_Workflow{destination: steps[len(steps)-1] + "_0"}
for i := 0; i < len(steps)-1; i++ {
attr := string(steps[i][0])
greater_than := string(steps[i][1])
value := strings.Split(strings.Split(steps[i], greater_than)[1], ":")[0]
destination := strings.Split(steps[i], ":")[1]
workflows[name+"_"+fmt.Sprintf("%d", i)] = Isolated_Workflow{
attr: attr,
greater_than: greater_than,
value: utils.IntegerOf(value),
destination: destination + "_0",
fail_con: name + "_" + fmt.Sprintf("%d", i+1),
}
}
}
return workflows
}
func parse_workflows(raw_workflows []string) map[string]Workflow {
worklows := make(map[string]Workflow)
for _, workflow := range raw_workflows {
name := strings.Split(workflow, "{")[0]
steps := strings.Split(strings.Trim(strings.Split(workflow, "{")[1], "}"), ",")
new_workflow := Workflow{final_destination: steps[len(steps)-1]}
for i := 0; i < len(steps)-1; i++ {
attr := string(steps[i][0])
greater_than := string(steps[i][1])
value := strings.Split(strings.Split(steps[i], greater_than)[1], ":")[0]
destination := strings.Split(steps[i], ":")[1]
new_workflow.conditions = append(new_workflow.conditions, Condition{
attr: attr,
greater_than: greater_than,
value: utils.IntegerOf(value),
destination: destination,
})
}
worklows[name] = new_workflow
}
return worklows
}
func part2(raw_workflows []string, raw_parts []string) {
defer utils.Timer("part2")()
worklows := parse_workflows2(raw_workflows)
initial_part := Part_Range{
values: map[string]Attr{
"x": {lower: 1, upper: 4000},
"m": {lower: 1, upper: 4000},
"a": {lower: 1, upper: 4000},
"s": {lower: 1, upper: 4000},
},
next_workflow: "in_0",
}
q := utils.Queue[Part_Range]{}
q.Push(initial_part)
accepted := []Part_Range{}
for !q.IsEmpty() {
part := q.Pop()
results := worklows[part.next_workflow].Process(part)
for _, result := range results {
if result.next_workflow == "A_0" {
accepted = append(accepted, result)
} else if result.next_workflow == "R_0" {
continue
} else {
q.Push(result)
}
}
fmt.Println(q)
}
result := 0
for _, part := range accepted {
part_result := (part.values["x"].upper - part.values["x"].lower + 1) * (part.values["m"].upper - part.values["m"].lower + 1) * (part.values["a"].upper - part.values["a"].lower + 1) * (part.values["s"].upper - part.values["s"].lower + 1)
result += part_result
}
fmt.Println(result)
}
func part1(raw_workflows []string, parts []string) {
defer utils.Timer("part1")()
worklows := parse_workflows(raw_workflows)
parsed_parts := []Part{}
for _, part := range parts {
part = strings.Trim(part, "{")
part = strings.Trim(part, "}")
sections := strings.Split(part, ",")
new_part := Part{}
total := 0
for _, section := range sections {
key, value := strings.Split(section, "=")[0], strings.Split(section, "=")[1]
total += utils.IntegerOf(value)
new_part[key] = utils.IntegerOf(value)
}
new_part["total"] = total
parsed_parts = append(parsed_parts, new_part)
}
result := 0
for _, part := range parsed_parts {
destination := worklows["in"].Process(part)
for destination != "A" && destination != "R" {
destination = worklows[destination].Process(part)
}
if destination == "A" {
result += part["total"]
}
}
fmt.Println(result)
}
func main() {
raw_input, _ := os.ReadFile("input.txt")
blocks := strings.Split(string(raw_input), "\n\n")
workflows := utils.RemoveSliceSpaces(strings.Split(blocks[0], "\n"))
parts := utils.RemoveSliceSpaces(strings.Split(blocks[1], "\n"))
part1(workflows, parts)
part2(workflows, parts)
}