Created
September 20, 2011 10:45
-
-
Save marick/1228842 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns scratch.core | |
(:use midje.sweet | |
[clojure.set :only [union intersection difference]] | |
)) | |
(def east [1 0]) | |
(def north [0 1]) | |
(def west [-1 0]) | |
(def south [0 -1]) | |
(defn rotate-anticlockwise [[x y]] | |
[(- y) x]) | |
(fact | |
(rotate-anticlockwise east) => north | |
(rotate-anticlockwise north) => west | |
(rotate-anticlockwise west) => south | |
(rotate-anticlockwise south) => east) | |
(defn rotate-clockwise [[x y]] | |
[y (- x)]) | |
(fact | |
(rotate-clockwise east) => south | |
(rotate-clockwise north) => east | |
(rotate-clockwise west) => north | |
(rotate-clockwise south) => west) | |
(defn forward [[position unit-vector]] | |
[(map + position unit-vector) unit-vector]) | |
(defn left [[position unit-vector]] | |
[position (rotate-anticlockwise unit-vector)]) | |
(defn right [[position unit-vector]] | |
[position (rotate-clockwise unit-vector)]) | |
(fact | |
(left [...position... east]) => [...position... north] | |
(right [...position... east]) => [...position... south]) | |
(defn every-star-is-visited? [stars visited-cells] | |
(empty? (difference stars (set visited-cells)))) | |
(defn level-passed? [& stuff] | |
(let [ {:keys [max-moves stars initial-direction moves]} (apply hash-map stuff) | |
route (reduce (fn [positions-so-far move] | |
(conj positions-so-far | |
(move (last positions-so-far)))) | |
[[ [0 0] initial-direction]] | |
(take max-moves moves))] | |
(every-star-is-visited? stars (map first route)))) | |
(def moves-irrelevent 1000) | |
;.;. Intellectual 'work' is misnamed; it is a pleasure, a dissipation, and | |
;.;. is its own highest reward. -- Twain | |
(facts | |
(level-passed? :max-moves moves-irrelevent | |
:stars #{ [1 0] } | |
:initial-direction east | |
:moves [forward]) => truthy | |
(level-passed? :max-moves moves-irrelevent | |
:stars #{ [-1 0] } | |
:initial-direction east | |
:moves [forward]) => falsey | |
(level-passed? :max-moves moves-irrelevent | |
:stars #{ [1 0] [2 0] } | |
:initial-direction east | |
:moves [forward forward]) => truthy | |
(level-passed? :max-moves moves-irrelevent | |
:stars #{ [0 1] [1 1] } | |
:initial-direction east | |
:moves [left forward right forward]) => truthy | |
(level-passed? :max-moves 3 | |
:stars #{ [0 1] [1 1] } | |
:initial-direction east | |
:moves [left forward right forward]) => falsey) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment