45 lines
813 B
Go
45 lines
813 B
Go
package allergies
|
|
|
|
// Allergies takes a score and returns all of the
|
|
// things that the score is allergic to.
|
|
func Allergies(score int) []string {
|
|
var ret []string
|
|
|
|
for _, v := range getAllAllergens() {
|
|
if AllergicTo(score, v) {
|
|
ret = append(ret, v)
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
// AllergicTo takes a score and an allergen and returns if
|
|
// That score is allergic to that thing.
|
|
func AllergicTo(score int, tst string) bool {
|
|
return score&getScoreForAllergen(tst) == getScoreForAllergen(tst)
|
|
}
|
|
|
|
func getAllAllergens() []string {
|
|
return []string{
|
|
"eggs",
|
|
"peanuts",
|
|
"shellfish",
|
|
"strawberries",
|
|
"tomatoes",
|
|
"chocolate",
|
|
"pollen",
|
|
"cats",
|
|
}
|
|
}
|
|
|
|
func getScoreForAllergen(tst string) int {
|
|
ret := 1
|
|
for _, v := range getAllAllergens() {
|
|
if tst == v {
|
|
return ret
|
|
}
|
|
ret *= 2
|
|
}
|
|
return -1
|
|
}
|