Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (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)
Add Comment
Please, Sign In to add comment