Skip to content

Instantly share code, notes, and snippets.

@tungd
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tungd/11372664 to your computer and use it in GitHub Desktop.
Save tungd/11372664 to your computer and use it in GitHub Desktop.
(ns logic-puzzle.core
(:refer-clojure :exclude [==])
(:require [clojure.core.logic :refer :all]))
(defn squaro [q]
(let [colors [:red :green :blue :yellow]]
(fresh [a1 a2 a3 a4
b1 b2 b3 b4]
(all
(permuteo colors [a1 a2 a3 a4])
(permuteo colors [b1 b2 b3 b4])
(!= a1 b1)
(!= a2 b2)
(!= a3 b3)
(!= a4 b4)
(== q [a1 a2 a3 a4 b1 b2 b3 b4])))))
(run 1 [q]
(squaro q))
;; => ([:red :green :blue :yellow :green :blue :yellow :red])
(count (run* [q] (squaro q)))
;; => 216
@tungd
Copy link
Author

tungd commented Apr 28, 2014

Each of the squares of two rows of squares (2x4) is coloured in one of 4 di�erent colours red, yellow, green and blue. Find the number of ways of colouring the two rows so that each row has 4 di�erent colours and no two adjacent squares are of the same colour. (Two squares are adjacent if they share a common side.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment