Skip to content

Instantly share code, notes, and snippets.

@kaykay
Created March 10, 2010 18:50
Show Gist options
  • Save kaykay/328199 to your computer and use it in GitHub Desktop.
Save kaykay/328199 to your computer and use it in GitHub Desktop.
(defn rotate [a]
(vec (conj (take (- (count a) 1) a) (last a))))
(defn get-at [coll ch]
(coll (- (int ch) 65)))
(defn rulesFor [level]
(let [rulz {1 [["1A" "2D"]] 2 [["2C" "3F"] ["1B" "3E"]]
3 [["3D" "4A"] ["1C" "4F"]] 4 [["4E" "5B"] ["1D" "5A"]]
5 [["5F" "6C"] ["1E" "6B"]] 6 [["6A" "7D"] ["1F" "7C"] ["2E" "7B"]]}]
(rulz level)))
(defn except-nth [coll n]
(if (= n 0)
(vec (drop 1 coll))
(vec (concat (subvec coll 0 n) (subvec coll (+ n 1))))))
(defn char-to-int [ch] (- (int ch) 48))
(defn rotate-till [hex req-val req-loc]
(if (= (get-at hex req-loc) req-val)
hex
(rotate-till (rotate hex) req-val req-loc)))
(defn matches? [arranged cur-hex rule]
(if (= (get-at (nth arranged (- (char-to-int (first (first rule))) 1)) (last (first rule)) )
(get-at cur-hex (last (last rule))))
true
false))
(defn hex-rotate [cur-hex rules arranged]
(rotate-till
cur-hex
(get-at (nth
arranged
(- (char-to-int (first (first (first rules)))) 1))
(last (first (first rules))) )
(last (last (first rules)))))
(defn satisfies [cur-hex rules arranged]
(if (nil? (first rules)) cur-hex
(let [current (count arranged)
rotated (hex-rotate cur-hex rules arranged)]
(loop [remaining-rules (rest rules)]
(if (nil? (first remaining-rules))
rotated
(if (matches? arranged rotated (first remaining-rules))
(recur (rest remaining-rules))
))))))
;(defn until-answer [coll func & args]
; (let [inner (fn [cnt
(defn arrange-level [arged arge]
(do (println (str "arranged : " arged)) (println (str "arrange: " arge))
(if (= (count arged) 7) arged
(let [level (count arged)]
(loop [cur-item 0 rged arged orig arge]
(do (println (str "current item : " cur-item))
(if (= cur-item (count orig)) nil
(let [sats (satisfies (nth orig cur-item) (rulesFor level) rged)]
(if sats
(let [scc (do (println "calling arrange-level: " (except-nth orig cur-item)) (arrange-level (conj rged sats) (except-nth orig cur-item)))]
(if scc scc
(do (println (str "orig:" orig)) (recur (inc cur-item) rged orig))))
(do (println (str "orig 2:" orig)) (recur (inc cur-item) rged orig)))))))))))
(defn arrange [hexs]
(arrange-level [] hexs))
(def hexagons [ [1 6 4 2 5 3]
[1 6 5 4 3 2]
[1 4 6 2 3 5]
[1 6 5 3 2 4]
[1 4 3 6 5 2]
[1 2 3 4 5 6]
[1 6 2 4 5 3] ])
(println (arrange hexagons))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment