Skip to content

Instantly share code, notes, and snippets.

@jboynyc
Created April 25, 2015 15:27
Show Gist options
  • Save jboynyc/495a6a482d294fba507d to your computer and use it in GitHub Desktop.
Save jboynyc/495a6a482d294fba507d to your computer and use it in GitHub Desktop.
What is Cheryl's birthday?
(def *possible-dates*
["May 15" "May 16" "May 19" "June 17" "June 18" "July 14" "July 16"
"August 14" "August 15" "August 17"])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn cheryls-birthday [possible-dates]
(filter all-statements possible-dates))
(defn month [date]
"return month part for dates like 'May 12'"
(first (.split date)))
(defn day [date]
"return day part for dates like 'May 12'"
(first (rest (.split date))))
(defn tell [part]
"possible dates based on known part"
(list (filter (fn [d] (in part d)) *possible-dates*)))
(defn know [possible-dates]
"is there only one possible date?"
(is (len possible-dates) 1))
(defn all-statements [date]
(and (statement-3 date)
(statement-4 date)
(statement-5 date)))
(defn statement-3 [date]
(let [[candidates (tell (month date))]]
(and (not (know candidates))
(all (map (fn [d] (not (know (tell (day d)))))
candidates)))))
(defn statement-4 [date]
(let [[at-first (tell (day date))]]
(and (not (know at-first))
(know (list (filter statement-3 at-first))))))
(defn statement-5 [date]
(know (list (filter statement-4 (tell (month date))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(let [[answer (list (cheryls-birthday *possible-dates*))]]
(print answer (know answer)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment