-
-
Save jabley/1228867 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 forward [[position unit-vector]] | |
[(map + position unit-vector) unit-vector]) | |
(defn left [[position unit-vector]] | |
[position (rotate-anticlockwise unit-vector)]) | |
(fact | |
(left [...position... east]) => [...position... north]) | |
(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]] | |
moves)] | |
(every-star-is-visited? stars (map first route)))) | |
(def moves-irrelevent 1000) | |
(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] } | |
:initial-direction east | |
:moves [left forward]) => truthy) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment