-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (155 loc) · 4.49 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"regexp"
"sort"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "21", "input.txt"))
if err != nil {
panic(err)
}
return input
}
var foodRegex = regexp.MustCompile(`^(.+) \(contains (.+)\)$`)
type constraint struct {
ingredients map[string]bool
allergens map[string]bool
}
func (c constraint) invalid(assignments map[string]string) bool {
applicableAllergens := make(map[string]bool)
applicableIngredients := make(map[string]bool)
for allergen, ingredient := range assignments {
if !c.allergens[allergen] {
continue
}
applicableAllergens[allergen] = true
if c.ingredients[ingredient] {
applicableIngredients[ingredient] = true
}
}
// not all allergens we care about have assignments yet, so we can't tell if it's invalid
// TODO: this can be expanded: e.g. if 1 is left to be assigned and we have 2 missing, it is invalid
if len(applicableAllergens) != len(c.allergens) {
return false
}
// all allergens we want are assigned, yet our ingredient list doesn't contain all of them
if len(applicableIngredients) < len(c.allergens) {
return true
}
return false
}
func constraintsInvalid(constraints []constraint, assignments map[string]string) bool {
for _, constraint := range constraints {
if constraint.invalid(assignments) {
return true
}
}
return false
}
func assign(allIngredients map[string]bool, allAllergens []string, assignments map[string]string, unassignments map[string]map[string]bool, constraints []constraint) bool {
if len(allAllergens) == 0 {
return true
}
allergenToAssign := allAllergens[0]
toTry := toSlice(allIngredients)
for _, ingredient := range toTry {
if unassignments[allergenToAssign][ingredient] {
continue
}
// prepare for future assignment
assignments[allergenToAssign] = ingredient
delete(allIngredients, ingredient)
unassignments[allergenToAssign] = make(map[string]bool)
for _, c := range constraints {
if !c.allergens[allergenToAssign] || !c.ingredients[ingredient] {
continue
}
for i := range c.ingredients {
if i == ingredient {
continue
}
unassignments[allergenToAssign][i] = true
}
}
// only keep going if we:
// - picked an assignment that's possible according to one of the rules
// - didn't invalidate any constraints with this assignment
if len(unassignments[allergenToAssign]) != 0 {
if !constraintsInvalid(constraints, assignments) {
if assign(allIngredients, allAllergens[1:], assignments, unassignments, constraints) {
return true
}
}
}
// unsuccessful, so undo
delete(assignments, allergenToAssign)
delete(unassignments, allergenToAssign)
allIngredients[ingredient] = true
}
return false
}
func solve(r io.Reader) {
scanner := bufio.NewScanner(r)
var constriants []constraint
allIngredients := make(map[string]bool)
allAllergens := make(map[string]bool)
for scanner.Scan() {
row := scanner.Text()
parts := foodRegex.FindStringSubmatch(row)
ingredients := strings.Split(parts[1], " ")
allergens := strings.Split(parts[2], ", ")
allergenMap := make(map[string]bool)
for _, allergen := range allergens {
allergenMap[allergen] = true
allAllergens[allergen] = true
}
ingredientsMap := make(map[string]bool)
for _, ingredient := range ingredients {
ingredientsMap[ingredient] = true
allIngredients[ingredient] = true
}
constriants = append(constriants, constraint{
ingredients: ingredientsMap,
allergens: allergenMap,
})
}
if scanner.Err() != nil {
panic(scanner.Err())
}
fmt.Println("ingredients", toSlice(allIngredients))
fmt.Println("allergens", toSlice(allAllergens))
assignments := make(map[string]string)
if !assign(allIngredients, toSlice(allAllergens), assignments, make(map[string]map[string]bool), constriants) {
panic("impossible")
}
fmt.Println(assignments)
sortedAllergens := toSlice(allAllergens)
sort.Strings(sortedAllergens)
var res strings.Builder
for i, allergen := range sortedAllergens {
res.WriteString(assignments[allergen])
if i != len(sortedAllergens)-1 {
res.WriteString(",")
}
}
fmt.Println(res.String())
}
func toSlice(m map[string]bool) []string {
ret := make([]string, len(m))
i := 0
for k := range m {
ret[i] = k
i += 1
}
return ret
}
func main() {
solve(strings.NewReader("mxmxvkd kfcds sqjhc nhms (contains dairy, fish)\ntrh fvjkl sbzzf mxmxvkd (contains dairy)\nsqjhc fvjkl (contains soy)\nsqjhc mxmxvkd sbzzf (contains fish)"))
solve(input())
}