Skip to content

Commit

Permalink
update example solution
Browse files Browse the repository at this point in the history
  • Loading branch information
tasxatzial committed Jun 27, 2024
1 parent d7ed0c0 commit cd66989
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions exercises/practice/allergies/.meta/src/example.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
(def ^:private allergens
[:eggs :peanuts :shellfish :strawberries :tomatoes :chocolate :pollen :cats])

(defn- flagged?
[flags index]
(-> (bit-shift-right flags index)
(bit-and 1)
(pos?)))

(defn allergies
"Given an 8-bit bitmap of flags, return the list of matching allergens."
[flags]
(keep-indexed (fn [index allergen]
(when (flagged? flags index)
allergen))
allergens))
(def ^:private allergen->power
(zipmap allergens (range (count allergens))))

(defn allergic-to?
"Given an 8-bit bitmap of flags and an allergen, return a boolean
indicating whether or not the patient is allergic to the given allergen."
[flags allergen]
(some #{allergen} (allergies flags)))
[score allergen]
(bit-test score (allergen->power allergen)))

(defn allergies
[score]
(keep-indexed #(if (bit-test score %1) %2) allergens))

0 comments on commit cd66989

Please sign in to comment.