Compare commits
5 Commits
7b68206577
...
64c9fd7b88
Author | SHA1 | Date |
---|---|---|
Daniel Barlow | 64c9fd7b88 | |
Daniel Barlow | 4b1f2ee923 | |
Daniel Barlow | 33243ca123 | |
Daniel Barlow | bcd833e26b | |
Daniel Barlow | 570fe8dadc |
130
rover.fnl
130
rover.fnl
|
@ -5,54 +5,74 @@
|
|||
(macro expect [text actual expected]
|
||||
`(let [actual# ,actual]
|
||||
(or (match actual#
|
||||
,expected true
|
||||
_# false)
|
||||
,expected true)
|
||||
(assert false (view {
|
||||
:text ,text
|
||||
:expected ,expected
|
||||
:expected ,(view expected)
|
||||
:actual actual#
|
||||
})))))
|
||||
|
||||
|
||||
(fn wrap-longitude [x]
|
||||
(if (<= x -180) (wrap-longitude (+ x 360))
|
||||
(< 180 x) (wrap-longitude (- x 360))
|
||||
x))
|
||||
|
||||
(fn drive [{: x : y : direction} distance]
|
||||
(match direction
|
||||
:n {:y (+ y distance)}
|
||||
:s {:y (- y distance)}
|
||||
:w {:x (- x distance)}
|
||||
:e {:x (+ x distance)}))
|
||||
(let [distance
|
||||
(if (. {:w true :e true} direction)
|
||||
(/ distance (math.cos (/ (* math.pi y) 180)))
|
||||
distance)]
|
||||
(match direction
|
||||
:n {:y (+ y distance)}
|
||||
:s {:y (- y distance)}
|
||||
:w {:x (wrap-longitude (- x distance))}
|
||||
:e {:x (wrap-longitude (+ x distance))})))
|
||||
|
||||
(fn turn-left [direction]
|
||||
{:direction
|
||||
(-> {
|
||||
:n :w
|
||||
:w :s
|
||||
:s :e
|
||||
:e :n
|
||||
}
|
||||
(. direction))})
|
||||
(fn turn-left [{: x : y : direction}]
|
||||
(if (= y 90)
|
||||
{:x (wrap-longitude (- x 90))}
|
||||
{:direction
|
||||
(-> {
|
||||
:n :w
|
||||
:w :s
|
||||
:s :e
|
||||
:e :n
|
||||
}
|
||||
(. direction))}))
|
||||
|
||||
(fn turn-right [direction]
|
||||
{:direction
|
||||
(-> {
|
||||
:n :e
|
||||
:e :s
|
||||
:s :w
|
||||
:w :n
|
||||
}
|
||||
(. direction))})
|
||||
(fn turn-right [{: x : y : direction}]
|
||||
(if (= y 90)
|
||||
{:x (wrap-longitude (+ x 90))}
|
||||
{:direction
|
||||
(-> {
|
||||
:n :e
|
||||
:e :s
|
||||
:s :w
|
||||
:w :n
|
||||
}
|
||||
(. direction))}))
|
||||
|
||||
(fn command [rover string]
|
||||
(merge rover
|
||||
(match string
|
||||
:f (drive rover 1)
|
||||
:b (drive rover -1)
|
||||
:r (turn-right rover.direction)
|
||||
:l (turn-left rover.direction)
|
||||
:r (turn-right rover)
|
||||
:l (turn-left rover)
|
||||
_ (assert false (. "unrecognised command " string)))))
|
||||
|
||||
(fn fudge-for-pole [{: y &as r}]
|
||||
(if (= y 90)
|
||||
(merge r {:direction :s})
|
||||
(= y -90)
|
||||
(merge r {:direction :n})
|
||||
r))
|
||||
|
||||
(fn execute [rover cmds]
|
||||
(accumulate [rover rover
|
||||
_ c (ipairs cmds)]
|
||||
(merge rover (command rover c))))
|
||||
(merge rover (fudge-for-pole (command rover c)))))
|
||||
|
||||
(fn rover [x y direction]
|
||||
{: x
|
||||
|
@ -96,9 +116,9 @@
|
|||
(expect "Moves north when pointing south and asked to move backward"
|
||||
(execute r [:b]) {:x 3 :y 3 :direction :s}))
|
||||
|
||||
(let [r (rover 3 2 :e)]
|
||||
(let [r (rover 3 0 :e)]
|
||||
(expect "Moves west when pointing east and asked to move backward"
|
||||
(execute r [:b]) {:x 2 :y 2 :direction :e}))
|
||||
(execute r [:b]) {:x 2 :y 0 :direction :e}))
|
||||
|
||||
(let [r (rover 2 4 :e)]
|
||||
(expect "Rotates to south when pointing east and asked to turn right"
|
||||
|
@ -114,8 +134,52 @@
|
|||
|
||||
(let [r (rover 1 1 :s)]
|
||||
(expect "Multiple commands are executed in the right order"
|
||||
(execute r [:l :l :f :f :f :r :f])
|
||||
{:x 2 :y 4 :direction :e}))
|
||||
(execute r [:l :l :f :f :f :r :f :b])
|
||||
{:x 1 :y 4 :direction :e}))
|
||||
|
||||
;; Circumference of mars is 3,376.2km (about half that of earth).
|
||||
;; We choose the unit of distance to be equal to one degree at the equator
|
||||
;; (about 60km), so if we travel the same linear distance at a higher
|
||||
;; latitude, we will move be a larger number of degrees
|
||||
|
||||
(expect "At 60 degrees latitude, one unit of drive is two degrees"
|
||||
(execute (rover 0 60 :e) [:f])
|
||||
(where {:x x :y 60 :direction :e}
|
||||
(< (math.abs (- x 2)) 0.000001)
|
||||
))
|
||||
|
||||
(expect "At 45 degrees latitude, one unit of drive is (sqrt 2) degrees"
|
||||
(execute (rover 0 45 :e) [:f])
|
||||
(where {:x x } (< (math.abs (- x 1.4141)) 0.001)))
|
||||
|
||||
(expect "At 45 degrees latitude south, one unit of drive is (sqrt 2) degrees"
|
||||
(execute (rover 0 -45 :e) [:f])
|
||||
(where {:x x :y -45} (< (math.abs (- x 1.4141)) 0.001)))
|
||||
|
||||
;; valid longitudes are -180 .. 180
|
||||
|
||||
(expect "Longitude wraps from positive to negative when travelling west"
|
||||
(execute (rover -179 0 :w) [:f :f :f])
|
||||
{:x 178 :y 0 :direction :w})
|
||||
|
||||
(expect "Longitude wraps from negative to positive when travelling east"
|
||||
(execute (rover 179 0 :e) [:f :f :f])
|
||||
{:x -178 :y 0 :direction :e})
|
||||
|
||||
(expect "At the North Pole we always point south"
|
||||
(execute (rover 0 89 :n) [:f])
|
||||
{:x 0 :y 90 :direction :s})
|
||||
|
||||
(expect "At the North Pole, turning left affects x not direction"
|
||||
(execute (rover 0 89 :n) [:f :l])
|
||||
{:x -90 :y 90 :direction :s})
|
||||
|
||||
(expect "At the North Pole, turning right affects x not direction"
|
||||
(execute (rover 0 89 :n) [:f :r])
|
||||
{:x 90 :y 90 :direction :s})
|
||||
|
||||
(expect "Near the North Pole, I can travel 90 degree in three steps"
|
||||
(execute (rover 0 89 :n) [:f :r :f])
|
||||
{:x 90 :y 89 :direction :s})
|
||||
|
||||
(print "OK")
|
||||
|
|
Loading…
Reference in New Issue