Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(ns puzzle
(:refer-clojure :exclude [==])
(:use [clojure.core.logic])
(:require [clojure.core.logic.set :as set]))
(defn existso [q ps] (fresh [x] (featurec x ps) (membero x q)))
(defn ruleo [q p v tp tv] (existso q {p v tp tv}))
(defn neg-ruleo [q p v tp tv] (fresh [x] (!= x tv)) (existso q {p v tp x}))
(defn earliero [q p v op ov]
(fresh [x y t0 t1]
(set/< t0 t1)
(existso q {p v :reserv t0})
(existso q {op ov :reserv t1})))
(defne peopleo [q ps]
([() _])
([[h . t] _]
(let [[k v] (first ps)]
(featurec h {k v}))
(peopleo t (next ps))))
(defn puzzle []
(let [vs (take 20 (repeatedly lvar))
ps (->> (partition 4 vs)
(map #(into {} (map vector [:name :cheese :mag :reserv] %)))
(into []))]
(run* [q]
(== q ps)
(everyg #(set/in % #{:amaya :bailey :jamari :jason :landon}) (map :name ps)
(everyg #(set/in % #{:asiago :blue :mascarpone :mozzarella :muenster}) (map :cheese ps)
(everyg #(set/in % #{:fortune :time :cosmopolitan :us-weekly :vogue}) (map :mag ps)
(everyg #(set/in % (sorted-set :5pm :6pm :7pm :730pm :830pm)) (map :reserv ps))
(everyg set/distinct (apply map vector (map vals ps)))
(conde
[(ruleo q :name :landon :reserv :730pm) (ruleo q :name :jason :cheese :mozzarella)]
[(ruleo q :name :landon :cheese :mozzarella) (ruleo q :name :jason :reserv :730pm)]) ;; 1
(ruleo q :cheese :blue :mag :fortune) ;; 2
(neg-ruleo q :cheese :muenster :mag :vogue) ;; 3
(peopleo q [[:mag :fortune] [:name :landon] [:reserv :5pm]
[:cheese :mascarpone] [:mag :vogue]]) ;; 4
(neg-ruleo q :reserv :5pm :mag :time)) ;; 5
(earliero q :mag :cosmopolitan :cheese :mascarpone) ;; 6
(earliero q :cheese :blue :name :bailey) ;; 7
(conde
[(ruleo q :reserv :7pm :mag :fortune)]
[(ruleo q :reserv :730pm :mag :fortune)]) ;; 8
(earliero q :mag :time :name :landon) ;; 9
(neg-ruleo q :name :jamari :mag :fortune)) ;; 10
(ruleo q :reserv :5pm :cheese :mozzarella)))) ;; 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment