rover.fnl (6293B)
1 (local { : view } (require :fennel)) 2 (local { : merge : any } (require :lume)) 3 4 (fn wrap-longitude [x] 5 (if (<= x -180) (wrap-longitude (+ x 360)) 6 (< 180 x) (wrap-longitude (- x 360)) 7 x)) 8 9 (fn at-obstacle? [{: x : y : obstacles}] 10 (any obstacles 11 (fn [[ox oy]] (and (= ox x) (= oy y))))) 12 13 (fn lateral? [direction] 14 (. {:w true :e true} direction)) 15 16 (fn try-to-drive [{: x : y : direction} distance] 17 (let [distance 18 (if (lateral? direction) 19 (/ distance (math.cos (/ (* math.pi y) 180))) 20 distance)] 21 (match direction 22 :n {:y (+ y distance)} 23 :s {:y (- y distance)} 24 :w {:x (wrap-longitude (- x distance))} 25 :e {:x (wrap-longitude (+ x distance))}))) 26 27 (fn drive [r distance] 28 (if r.stopped 29 r 30 (let [next-move (merge r (try-to-drive r distance))] 31 (if (at-obstacle? next-move) 32 (merge r {:stopped [next-move.x next-move.y]}) 33 next-move)))) 34 35 (fn turn-left [{: x : y : direction}] 36 (if (= y 90) 37 {:x (wrap-longitude (- x 90))} 38 {:direction 39 (-> { 40 :n :w 41 :w :s 42 :s :e 43 :e :n 44 } 45 (. direction))})) 46 47 (fn turn-right [{: x : y : direction}] 48 (if (= y 90) 49 {:x (wrap-longitude (+ x 90))} 50 {:direction 51 (-> { 52 :n :e 53 :e :s 54 :s :w 55 :w :n 56 } 57 (. direction))})) 58 59 (fn command [rover string] 60 (merge rover 61 (match string 62 :f (drive rover 1) 63 :b (drive rover -1) 64 :r (turn-right rover) 65 :l (turn-left rover) 66 _ (assert false (. "unrecognised command " string))))) 67 68 (fn fudge-for-pole [{: y &as r}] 69 (if (= y 90) 70 (merge r {:direction :s}) 71 (= y -90) 72 (merge r {:direction :n}) 73 r)) 74 75 (fn execute [rover cmds] 76 (accumulate [rover rover 77 _ c (ipairs cmds)] 78 (merge rover (fudge-for-pole (command rover c))))) 79 80 (fn rover [x y direction ?obstacles] 81 {: x 82 : y 83 : direction 84 :obstacles (or ?obstacles []) 85 }) 86 87 ;;;; TESTS 88 89 ;; who needs a test framework when you have lisp macros? 90 (macro expect [text actual expected] 91 `(let [actual# ,actual] 92 (or (match actual# 93 ,expected true) 94 (assert false (view { 95 :text ,text 96 :expected ,(view expected) 97 :actual actual# 98 }))))) 99 100 (let [r (rover 7 15 :n)] 101 (expect 102 "rover knows (x,y) and the direction (N,S,E,W) it is facing" 103 r 104 {:x 7 :y 15 :direction :n})) 105 106 (let [r (rover 0 0 :n)] 107 (expect "The rover receives a character array of commands" 108 (execute r [:f :f :f]) 109 {})) 110 111 (let [r (rover 0 0 :n)] 112 (expect "Moves north when pointing north and asked to move forward" 113 (execute r [:f]) {:x 0 :y 1 :direction :n})) 114 115 (let [r (rover 3 2 :s)] 116 (expect "Moves south when pointing south and asked to move forward" 117 (execute r [:f]) {:x 3 :y 1 :direction :s})) 118 119 (let [r (rover 0 0 :w)] 120 (expect "Moves west when pointing west and asked to move forward" 121 (execute r [:f]) {:x -1 :y 0 :direction :w})) 122 123 (let [r (rover 0 0 :e)] 124 (expect "Moves east when pointing east and asked to move forward" 125 (execute r [:f]) {:x 1 :y 0 :direction :e})) 126 127 (let [r (rover 1 1 :s)] 128 (expect "The rover acts on multiple commands" 129 (execute r [:f :f :f]) 130 {:x 1 :y -2 :direction :s})) 131 132 (let [r (rover 3 2 :s)] 133 (expect "Moves north when pointing south and asked to move backward" 134 (execute r [:b]) {:x 3 :y 3 :direction :s})) 135 136 (let [r (rover 3 0 :e)] 137 (expect "Moves west when pointing east and asked to move backward" 138 (execute r [:b]) {:x 2 :y 0 :direction :e})) 139 140 (let [r (rover 2 4 :e)] 141 (expect "Rotates to south when pointing east and asked to turn right" 142 (execute r [:r]) {:x 2 :y 4 :direction :s})) 143 144 (let [r (rover 2 4 :s)] 145 (expect "Rotates to east when pointing south and asked to turn left" 146 (execute r [:l]) {:x 2 :y 4 :direction :e})) 147 148 (let [r (rover 2 4 :w)] 149 (expect "Rotates to north when pointing west and asked to turn right" 150 (execute r [:r]) {:x 2 :y 4 :direction :n})) 151 152 (let [r (rover 1 1 :s)] 153 (expect "Multiple commands are executed in the right order" 154 (execute r [:l :l :f :f :f :r :f :b]) 155 {:x 1 :y 4 :direction :e})) 156 157 ;; Circumference of mars is 3,376.2km (about half that of earth). 158 ;; We choose the unit of distance to be equal to one degree at the equator 159 ;; (about 60km), so if we travel the same linear distance at a higher 160 ;; latitude, we will move be a larger number of degrees 161 162 (expect "At 60 degrees latitude, one unit of drive is two degrees" 163 (execute (rover 0 60 :e) [:f]) 164 (where {:x x :y 60 :direction :e} 165 (< (math.abs (- x 2)) 0.000001) 166 )) 167 168 (expect "At 45 degrees latitude, one unit of drive is (sqrt 2) degrees" 169 (execute (rover 0 45 :e) [:f]) 170 (where {:x x } (< (math.abs (- x 1.4141)) 0.001))) 171 172 (expect "At 45 degrees latitude south, one unit of drive is (sqrt 2) degrees" 173 (execute (rover 0 -45 :e) [:f]) 174 (where {:x x :y -45} (< (math.abs (- x 1.4141)) 0.001))) 175 176 ;; valid longitudes are -180 .. 180 177 178 (expect "Longitude wraps from positive to negative when travelling west" 179 (execute (rover -179 0 :w) [:f :f :f]) 180 {:x 178 :y 0 :direction :w}) 181 182 (expect "Longitude wraps from negative to positive when travelling east" 183 (execute (rover 179 0 :e) [:f :f :f]) 184 {:x -178 :y 0 :direction :e}) 185 186 (expect "At the North Pole we always point south" 187 (execute (rover 0 89 :n) [:f]) 188 {:x 0 :y 90 :direction :s}) 189 190 (expect "At the North Pole, turning left affects x not direction" 191 (execute (rover 0 89 :n) [:f :l]) 192 {:x -90 :y 90 :direction :s}) 193 194 (expect "At the North Pole, turning right affects x not direction" 195 (execute (rover 0 89 :n) [:f :r]) 196 {:x 90 :y 90 :direction :s}) 197 198 (expect "It does not move past obstacles" 199 (let [obstacles [[5 5] [7 5]]] 200 (execute (rover 5 3 :n obstacles) [:f :f :f :f :f :f])) 201 {:x 5 :y 4 :direction :n :stopped [5 5]}) 202 203 204 ;; "TODO: deal with the south pole special casing" 205 206 (print "OK")