Skip to content

Instantly share code, notes, and snippets.

@jabley
Forked from marick/gist:1228842
Created September 20, 2011 11:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabley/1228867 to your computer and use it in GitHub Desktop.
Save jabley/1228867 to your computer and use it in GitHub Desktop.
(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