Skip to content

Instantly share code, notes, and snippets.

@Engelberg
Created March 7, 2013 05:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Engelberg/5105806 to your computer and use it in GitHub Desktop.
Save Engelberg/5105806 to your computer and use it in GitHub Desktop.
Solving logic puzzle with for comprehension
(defn solve-logic-puzzle []
(let [people [:amaya :bailey :jamari :jason :landon]]
(first
(for [[fortune time cosmopolitan us-weekly vogue] (permutations people) ; magazines
[asiago blue-cheese mascarpone mozzarella muenster] (permutations people) ; cheeses
; We bind the reservations in two steps, so we have a name for the overall order
reservations (permutations people)
:let [[five six seven seven-thirty eight-thirty] reservations]
; THE CONSTRAINTS IN PLAIN ENGLISH
; Of Landon and Jason, one has the 7:30pm reservation and the other loves mozzarella.
; The blue cheese enthusiast subscribed to Fortune.
; The muenster enthusiast didn't subscribe to Vogue.
; The 5 people were the Fortune subscriber, Landon, the person with a reservation at 5:00pm, the mascarpone enthusiast, and the Vogue subscriber.
; The person with a reservation at 5:00pm didn't subscribe to Time.
; The Cosmopolitan subscriber has an earlier reservation than the mascarpone enthusiast.
; Bailey has a later reservation than the blue cheese enthusiast.
; Either the person with a reservation at 7:00pm or the person with a reservation at 7:30pm subscribed to Fortune.
; Landon has a later reservation than the Time subscriber.
; The Fortune subscriber is not Jamari.
; The person with a reservation at 5:00pm loves mozzarella.
; THE CONSTRAINTS IN CLOJURE (in the same order)
:when (= (set [seven-thirty mozzarella]) (set [:landon :jason]))
:when (= blue-cheese fortune)
:when (not= muenster vogue)
:when (= (count (set [fortune :landon five mascarpone vogue])) 5)
:when (not= five time)
:when (< (.indexOf reservations cosmopolitan) (.indexOf reservations mascarpone))
:when (> (.indexOf reservations :bailey) (.indexOf reservations blue-cheese))
:when (#{seven seven-thirty} fortune)
:when (> (.indexOf reservations :landon) (.indexOf reservations time))
:when (not= fortune :jamari)
:when (= five mozzarella)]
; RETURN THE ANSWER
(array-map
:fortune fortune :time time :cosmopolitan cosmopolitan :us-weekly us-weekly :vogue vogue
:asiago asiago :blue-cheese blue-cheese :mascarpone mascarpone :mozzarella mozzarella :muenster muenster
:five five :six six :seven seven :seven-thirty seven-thirty :eight-thirty eight-thirty)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment