Skip to content

Instantly share code, notes, and snippets.

@jacquesattack
Created December 30, 2019 20:23
Show Gist options
  • Save jacquesattack/bb79d7e87a5bc48445d87e88618e8714 to your computer and use it in GitHub Desktop.
Save jacquesattack/bb79d7e87a5bc48445d87e88618e8714 to your computer and use it in GitHub Desktop.
Solve Stauf Painting Problem
;; stauf painting problem
(defn get-move [x]
(case x
0 '(1 1 0
1 1 0
0 0 0)
1 '(1 1 1
0 0 0
0 0 0)
2 '(0 1 1
0 1 1
0 0 0)
3 '(1 0 0
1 0 0
1 0 0)
4 '(0 1 0
1 1 1
0 1 0)
5 '(0 0 1
0 0 1
0 0 1)
6 '(0 0 0
1 1 0
1 1 0)
7 '(0 0 0
0 0 0
1 1 1)
8 '(0 0 0
0 1 1
0 1 1)))
;; be sure to use 'doall' because map is lazy and if you do not realize the sequences, the stack will overflow
(defn new-puzzle [puzzle move]
(doall (map #(mod (+ %1 %2) 3) puzzle (get-move move))))
(defn has-3 [moves value]
(= (mod (count (filter #(= % value) moves)) 3) 0))
(defn update-moves [moves value]
(if (has-3 moves value)
(remove #(= % value) moves)
moves))
(defn solve-stauf [init-puzzle]
(loop [puzzle init-puzzle
moves '()]
(if (= puzzle (repeat 9 0))
(sort moves)
(let [move-int (rand-int 9)
new-moves (update-moves (conj moves move-int) move-int)
next-puzzle (new-puzzle puzzle move-int)]
(recur next-puzzle new-moves)))))
;; user=> (solve-stauf '(1 1 1 0 1 1 1 2 2))
;; (1 1 7 7 8 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment