Guest User

Untitled

a guest
Jan 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. (ns scratch.core
  2. (:use midje.sweet
  3. [clojure.set :only [union intersection difference]]
  4. ))
  5.  
  6.  
  7. (def east [1 0])
  8. (def north [0 1])
  9. (def west [-1 0])
  10. (def south [0 -1])
  11.  
  12. (defn rotate-anticlockwise [[x y]]
  13. [(- y) x])
  14.  
  15. (fact
  16. (rotate-anticlockwise east) => north
  17. (rotate-anticlockwise north) => west
  18. (rotate-anticlockwise west) => south
  19. (rotate-anticlockwise south) => east)
  20.  
  21.  
  22. (defn forward [[position unit-vector]]
  23. [(map + position unit-vector) unit-vector])
  24. (defn left [[position unit-vector]]
  25. [position (rotate-anticlockwise unit-vector)])
  26.  
  27.  
  28. (fact
  29. (left [...position... east]) => [...position... north])
  30.  
  31. (defn every-star-is-visited? [stars visited-cells]
  32. (empty? (difference stars (set visited-cells))))
  33.  
  34. (defn level-passed? [& stuff]
  35. (let [ {:keys [max-moves stars initial-direction moves]} (apply hash-map stuff)
  36. route (reduce (fn [positions-so-far move]
  37. (conj positions-so-far
  38. (move (last positions-so-far))))
  39. [[ [0 0] initial-direction]]
  40. moves)]
  41. (every-star-is-visited? stars (map first route))))
  42.  
  43. (def moves-irrelevent 1000)
  44.  
  45. (facts
  46. (level-passed? :max-moves moves-irrelevent
  47. :stars #{ [1 0] }
  48. :initial-direction east
  49. :moves [forward]) => truthy
  50.  
  51. (level-passed? :max-moves moves-irrelevent
  52. :stars #{ [-1 0] }
  53. :initial-direction east
  54. :moves [forward]) => falsey
  55.  
  56. (level-passed? :max-moves moves-irrelevent
  57. :stars #{ [1 0] [2 0] }
  58. :initial-direction east
  59. :moves [forward forward]) => truthy
  60.  
  61. (level-passed? :max-moves moves-irrelevent
  62. :stars #{ [0 1] }
  63. :initial-direction east
  64. :moves [left forward]) => truthy)
Add Comment
Please, Sign In to add comment