-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
174 lines (149 loc) · 3.52 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
package main
import (
"bufio"
"io"
"sort"
"strings"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
var r []Recipe
for scanner.Scan() {
line := scanner.Text()
r = append(r, toRecipe(line))
}
recipes := make(map[string][]map[string]bool)
for _, recipe := range r {
for _, allergen := range recipe.allergens {
m := make(map[string]bool)
for _, v := range recipe.ingredients {
m[v] = true
}
recipes[allergen] = append(recipes[allergen], m)
}
}
remainingAllergens := make(map[string]bool)
for k := range recipes {
remainingAllergens[k] = true
}
foundAllergenForIngredient := make(map[string]string)
for len(remainingAllergens) != 0 {
for allergen := range remainingAllergens {
intersections := findIntersections(recipes[allergen])
if len(intersections) == 1 {
delete(remainingAllergens, allergen)
foundAllergenForIngredient[intersections[0]] = allergen
for _, rcps := range recipes {
for _, rcp := range rcps {
delete(rcp, intersections[0])
}
}
}
}
}
sum := 0
for _, recipe := range r {
for _, ingredient := range recipe.ingredients {
if _, exists := foundAllergenForIngredient[ingredient]; !exists {
sum++
}
}
}
return sum
}
func findIntersections(recipes []map[string]bool) []string {
contains := make(map[string]bool)
for i := 0; i < len(recipes[0]); i++ {
for ingredient := range recipes[0] {
contains[ingredient] = true
}
}
for i := 1; i < len(recipes); i++ {
m := make(map[string]bool)
for k := range recipes[i] {
m[k] = true
}
for k := range contains {
if !m[k] {
delete(contains, k)
}
}
}
var res []string
for k := range contains {
res = append(res, k)
}
return res
}
type Recipe struct {
ingredients []string
allergens []string
}
func toRecipe(s string) Recipe {
sep := strings.Index(s, "(")
ingredients := aoc.NewDelimiter(s[:sep-1], " ").GetStrings()
contains := aoc.NewDelimiter(s[sep+10:len(s)-1], ", ").GetStrings()
return Recipe{
ingredients: ingredients,
allergens: contains,
}
}
func fs2(input io.Reader) string {
scanner := bufio.NewScanner(input)
var r []Recipe
for scanner.Scan() {
line := scanner.Text()
r = append(r, toRecipe(line))
}
recipes := make(map[string][]map[string]bool)
for _, recipe := range r {
for _, allergen := range recipe.allergens {
m := make(map[string]bool)
for _, v := range recipe.ingredients {
m[v] = true
}
recipes[allergen] = append(recipes[allergen], m)
}
}
remainingAllergens := make(map[string]bool)
for k := range recipes {
remainingAllergens[k] = true
}
foundAllergenForIngredient := make(map[string]string)
for len(remainingAllergens) != 0 {
for allergen := range remainingAllergens {
intersections := findIntersections(recipes[allergen])
if len(intersections) == 1 {
delete(remainingAllergens, allergen)
foundAllergenForIngredient[intersections[0]] = allergen
for _, rcps := range recipes {
for _, rcp := range rcps {
delete(rcp, intersections[0])
}
}
}
}
}
var founds []Found
for k, v := range foundAllergenForIngredient {
founds = append(founds, Found{
allergen: v,
ingredient: k,
})
}
sort.Slice(founds, func(i, j int) bool {
a := founds[i]
b := founds[j]
return strings.Compare(a.allergen, b.allergen) < 0
})
var res []string
for _, found := range founds {
res = append(res, found.ingredient)
}
return strings.Join(res, ",")
}
type Found struct {
allergen string
ingredient string
}