-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday21.go
More file actions
181 lines (146 loc) · 4.67 KB
/
day21.go
File metadata and controls
181 lines (146 loc) · 4.67 KB
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
package main
import (
"fmt"
"sort"
"strings"
)
func removeListItem(list []string, itemToFind string) (result []string) {
result = make([]string, 0)
for _, item := range list {
if item != itemToFind {
result = append(result, item)
}
}
return result
}
func itemInList(list []string, itemToFind string) bool {
for _, item := range list {
if item == itemToFind {
return true
}
}
return false
}
func joinedLists(first []string, second []string) (result []string) {
result = make([]string, 0)
for _, item := range first {
if itemInList(second, item) {
// item is in both lists
result = append(result, item)
}
}
return result
}
func workOutFoods(foodSource []string, debug bool) (ingredients []string, foundAllergens map[string]string, foundIngredients map[string]string) {
ingredients = make([]string, 0)
workingAllergens := make(map[string][]string)
// Hedging bets for part 2. Let's build a map of allergen to ingredients (foundAllergens) and ingredients to allergens (foundIngredients)
foundAllergens = make(map[string]string)
foundIngredients = make(map[string]string)
for _, line := range foodSource {
if debug {
fmt.Println("============================================")
fmt.Println("Line:", line)
}
allergens := strings.SplitAfter(line, "(contains ")
lineIngredients := strings.Split(strings.TrimSuffix(allergens[0], " (contains "), " ")
lineAllergens := strings.Split(strings.TrimSuffix(allergens[1], ")"), ", ")
if debug {
fmt.Printf("Ingredients: %s\n", lineIngredients)
fmt.Printf("Allergens: %s\n", lineAllergens)
}
for _, allergen := range lineAllergens {
if debug {
fmt.Println("Allergen:", allergen)
}
// Now store the ingredients against each allergen.
if _, ok := workingAllergens[allergen]; ok {
// we've seen this before. Reduce list to the common items between previous and current lines
workingAllergens[allergen] = joinedLists(workingAllergens[allergen], lineIngredients)
} else {
workingAllergens[allergen] = lineIngredients
}
}
for _, i := range lineIngredients {
ingredients = append(ingredients, i)
}
}
var keepLooping bool = true
for keepLooping {
// Work through the workingAllergens looking for matches where an allergen has a single ingredient
// When we find one, record that in the foundAllergens map and remove from workingAllergens
for allergen, ingredients := range workingAllergens {
if len(ingredients) == 1 {
// If there's only 1 ingredient we've found a match
foundAllergens[allergen] = ingredients[0]
foundIngredients[ingredients[0]] = allergen
// Now need to remove this allergen from workging Allergens
delete(workingAllergens, allergen)
for remainingAllergen, remainingIngredients := range workingAllergens {
workingAllergens[remainingAllergen] = removeListItem(remainingIngredients, ingredients[0])
}
break
}
}
if len(workingAllergens) == 0 {
// Found them all
keepLooping = false
}
}
if debug {
fmt.Println("Found allergens:", foundAllergens)
fmt.Println(workingAllergens)
}
return ingredients, foundAllergens, foundIngredients
}
// part b
// Go through the foundAllergens list. Build a list of strings containing the dangerous ingredients
// Sort it, then join all the strings together and return
func canonicalIngredientList(filename string, debug bool) string {
var tmpAllergensList []string
var tmpBuildResult []string
puzzleInput, _ := readFile(filename)
_, foundAllergens, _ := workOutFoods(puzzleInput, debug)
if debug {
fmt.Println("Found allergens map:", foundAllergens)
}
for allergen := range foundAllergens {
tmpAllergensList = append(tmpAllergensList, allergen)
}
sort.Strings(tmpAllergensList)
for _, allergen := range tmpAllergensList {
tmpBuildResult = append(tmpBuildResult, foundAllergens[allergen])
}
return strings.Join(tmpBuildResult, ",")
}
// part a
func countNonAllergens(filename string, debug bool) int {
var result int
puzzleInput, _ := readFile(filename)
ingredients, _, foundIngredients := workOutFoods(puzzleInput, debug)
if debug {
fmt.Println("Ingredients array:", ingredients)
fmt.Println("Found ingredients map:", foundIngredients)
}
result = 0
for _, i := range ingredients {
if _, ok := foundIngredients[i]; !ok {
result++
}
}
return result
}
// Main routine
func main() {
filenamePtr, execPart, debug, test := catchUserInput()
if test {
return
}
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else if execPart == 'a' {
fmt.Println("Non Allergens appear:", countNonAllergens(filenamePtr, debug))
} else {
fmt.Println("Caconical List:", canonicalIngredientList(filenamePtr, debug))
}
}