Guest User

Untitled

a guest
Sep 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. (ns codelesson.week3-1
  2. (:require [clojure.string])
  3. (:gen-class))
  4.  
  5. (defn merge-with-padding [x y]
  6. " merge arrays x & y and padding the shorter of the two with nils "
  7. (let [cx (count x) cy (count y)]
  8. (if (not= cx cy)
  9. (if (< cx cy)
  10. (map #(vector %1 %2) (concat x (take (- cy cx) (repeat nil) )) y)
  11. (map #(vector %1 %2) x (concat y (take (- cx cy) (repeat nil) ))))
  12. (map #(vector %1 %2) x y))))
  13.  
  14. (defn char-to-i [c]
  15. " cheezy character to int coverter "
  16. (- (int c) 48))
  17.  
  18. (defn coord [thegrid x y]
  19. (let [gridy (dec (count thegrid))]
  20. ((thegrid (- gridy y)) x)))
  21.  
  22. (defn get-rover-coords [rover-num]
  23. (println "Enter rover" rover-num " initial coords")
  24. (let [in (read-line)
  25. v (filter #(not= \space %) (vec in))] ; [\1 \2 \N]
  26. {:name (keyword (str rover-num)) :position [(char-to-i (first v)) (char-to-i (second v))] :facing (last v)}))
  27.  
  28. (defn get-grid-size []
  29. (println "Enter the grid dimensions")
  30. (let [in (read-line)]
  31. [ (char-to-i (first in)) (char-to-i (last in)) ]))
  32.  
  33. (defn get-rover-mvmt [rover-num]
  34. (println "Enter movement instructions for rover" rover-num)
  35. (read-line))
  36.  
  37. (defn set-coord [thegrid rover contents]
  38. "returns grid with rover at it's :position"
  39. (let [gridy (dec (count thegrid)) x (first (:position rover)) y (last (:position rover))]
  40. (assoc-in thegrid [(- gridy y) x] contents)))
  41. ;(assoc-in thegrid (:position rover) rover))
  42.  
  43. (defn create-grid [x y rover1 rover2]
  44. (let [row (vec (repeat (inc y) {}))
  45. grid (vec (repeat (inc x) row))
  46. grid (set-coord grid rover1 rover1)
  47. grid (set-coord grid rover2 rover2)]
  48. grid))
  49.  
  50. (def turn-left {\E \N \N \W \W \S \S \E})
  51. (def turn-right {\E \S \S \W \W \N \N \E})
  52. (def turn {\L turn-left \R turn-right})
  53.  
  54. (defn forward [x y facing rovername]
  55. "returns new {:position [x y] :facing facing} after moving forward 1-coordinate"
  56. (condp = facing
  57. \N {:position [x (inc y)] :facing facing :name rovername}
  58. \S {:position [x (dec y)] :facing facing :name rovername}
  59. \E {:position [(inc x) y] :facing facing :name rovername}
  60. \W {:position [(dec x) y] :facing facing :name rovername}
  61. nil))
  62.  
  63. (defn rover-moved [rover move]
  64. " returns a new rover map after the move"
  65. (let [x (first (:position rover)) y (last (:position rover)) facing (:facing rover) rovername (:name rover)]
  66. (cond
  67. (or (= move \L) (= move \R)) {:position [x y] :facing ((turn move) facing) :name rovername}
  68. (= move \M) (forward x y facing rovername)
  69. (nil? move) rover
  70. :default (throw (do (println "Shouldn't hit this in rover-moved!"))))))
  71.  
  72. (defn not-in-bounds? [rover grid]
  73. (let [x (first (:position rover)) y (last (:position rover))
  74. size-x (count grid) size-y (count (get grid 0))]
  75. (if (and (< x size-x) (< y size-y) (>= x 0) (>= y 0))
  76. false
  77. true)))
  78.  
  79. (defn check-moves [r1new r2new grid]
  80. (cond
  81. (not-in-bounds? r1new @grid) [false "Rover 1 out of bounds!"]
  82. (not-in-bounds? r2new @grid) [false "Rover 2 out of bounds!"]
  83. (= (:position r1new) (:position r2new)) [false "Crash!!!!!!"]
  84. :default [true true]))
  85.  
  86. (defn update-grid [grid r1 r1new r2 r2new]
  87. (dosync
  88. (send grid set-coord r1 {})
  89. (send grid set-coord r1new r1new)
  90. (send grid set-coord r2 {})
  91. (send grid set-coord r2new r2new)
  92. [r1new r2new]))
  93.  
  94. (defn parse-move [state move grid]
  95. " returns [{rover1map} {rover2map} [grid]]"
  96. (let [r1move (first move)
  97. r2move (last move)
  98. r1 (first state)
  99. r2 (second state)
  100. r1new (rover-moved r1 r1move)
  101. r2new (rover-moved r2 r2move)
  102. validmove (check-moves r1new r2new grid)]
  103. (if (first validmove)
  104. (update-grid grid r1 r1new r2 r2new)
  105. validmove)))
  106.  
  107. (defn showgrid [grid]
  108. (await grid)
  109. (doseq [row @grid]
  110. (prn row)))
  111.  
  112. (defn spit-state [state grid]
  113. (let [rover1 (first state) rover2 (second state)]
  114. (println (first (:position rover1)) (last (:position rover1)) (:facing rover1))
  115. (println (first (:position rover2)) (last (:position rover2)) (:facing rover2))
  116. (showgrid grid)))
  117.  
  118. (defn spit-error [grid error]
  119. (showgrid grid)
  120. (println error))
  121.  
  122. (defn rove [grid-x grid-y rover1-initial-pos rover1-moves rover2-initial-pos rover2-moves]
  123. (let [ grid (agent (create-grid grid-x grid-y rover1-initial-pos rover2-initial-pos))]
  124. (loop [rover-state [rover1-initial-pos rover2-initial-pos ]
  125. current-move (first (merge-with-padding rover1-moves rover2-moves))
  126. moves (rest (merge-with-padding rover1-moves rover2-moves))]
  127. (if (and (not-empty current-move) (first rover-state))
  128. (recur (parse-move rover-state current-move grid) (first moves) (rest moves))
  129. (if (false? (first rover-state))
  130. (spit-error grid (last rover-state))
  131. (spit-state rover-state grid))))))
  132.  
  133. (defn -main[] ; works with 'lein trampoline run'
  134. (let [grid-dims (get-grid-size)
  135. rover1-pos (get-rover-coords 1)
  136. rover1-mvmt (get-rover-mvmt 1)
  137. rover2-pos (get-rover-coords 2)
  138. rover2-mvmt (get-rover-mvmt 2)]
  139. (rove (first grid-dims) (last grid-dims) rover1-pos rover1-mvmt rover2-pos rover2-mvmt)))
  140.  
  141. (rove 5 5 {:name :rover1 :position [1 2] :facing \N} "LMLMLMLMM" {:name :rover2 :position [3 3] :facing \E} "MMRMMRMRRM")
  142. (rove 3 3 {:name :rover1 :position [1 2] :facing \N} "LMLMLMLMM" {:name :rover2 :position [3 3] :facing \E} "MMRMMRMRRM") ; contrive out of bounds
  143. (rove 3 3 {:name :rover1 :position [2 0] :facing \N} "MMMMMMM" {:name :rover2 :position [0 2] :facing \E} "MMMMRRRM") ; contrive a crash!
Add Comment
Please, Sign In to add comment