-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.go
135 lines (112 loc) · 2.83 KB
/
day21.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
package main
import (
"fmt"
"io/ioutil"
"sort"
"strings"
)
type Food struct {
Ingredients []string
Allergens []string
}
func ArrayContains(arr []string, value string) bool {
for _, k := range arr {
if k == value {
return true
}
}
return false
}
func FindAllergen(foods []Food, allergen string) string {
foods_that_contain := []Food{}
for _, food := range foods {
if ArrayContains(food.Allergens, allergen) {
foods_that_contain = append(foods_that_contain, food)
}
}
counts := make(map[string]int)
for _, food := range foods_that_contain {
for _, ing := range food.Ingredients {
counts[ing]++
}
}
counts_that_match := 0
name_that_match := ""
for name, count := range counts {
if string(name[0]) != "!" && count == len(foods_that_contain) {
counts_that_match++
name_that_match = name
}
}
if counts_that_match > 1 {
return ""
}
return name_that_match
}
func AdjustNames(foods []Food, allergen string, real_name string) []Food {
new_foods := []Food{}
for _, food := range foods {
if ArrayContains(food.Ingredients, real_name) {
for idx, ing := range food.Ingredients {
if ing == real_name {
// weird hack the FindAllergen function knows that this is found already
food.Ingredients[idx] = "!" + allergen
}
}
}
new_foods = append(new_foods, food)
}
return new_foods
}
func day21() {
inp, _ := ioutil.ReadFile("./inputs/day21.input")
data := GetStringInput(inp)
allergens := make(map[string]string)
ingredients := make(map[string]string)
foods := []Food{}
for _, line := range data {
contains_split := strings.Split(line, " (contains ")
ings := strings.Split(contains_split[0], " ")
als := strings.Split(strings.Trim(contains_split[1], ")"), ", ")
for _, a := range als {
allergens[a] = "unknown"
}
for _, i := range ings {
ingredients[i] = "unknown"
}
foods = append(foods, Food{ings, als})
}
fmt.Println("foods:", len(foods), "i:", len(ingredients), "a:", len(allergens))
fmt.Println(allergens)
still_going := true
for still_going {
still_going = false
for a, _ := range allergens {
real_name := FindAllergen(foods, a)
if real_name != "" {
foods = AdjustNames(foods, a, real_name)
fmt.Println("setting", a, "to", real_name)
allergens[a] = real_name
still_going = true
}
}
}
count_non_allergens := 0
for _, food := range foods {
for _, ings := range food.Ingredients {
if string(ings[0]) != "!" {
count_non_allergens++
}
}
}
fmt.Println(count_non_allergens)
// gather all the allergens to sort them later
algs := []string{}
for k := range allergens {
algs = append(algs, k)
}
sort.Strings(algs)
for _, ag := range algs {
fmt.Printf("%v,", allergens[ag])
}
}