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]
|
(macro expect [text actual expected]
|
||||||
`(let [actual# ,actual]
|
`(let [actual# ,actual]
|
||||||
(or (match actual#
|
(or (match actual#
|
||||||
,expected true
|
,expected true)
|
||||||
_# false)
|
|
||||||
(assert false (view {
|
(assert false (view {
|
||||||
:text ,text
|
:text ,text
|
||||||
:expected ,expected
|
:expected ,(view expected)
|
||||||
:actual actual#
|
: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]
|
(fn drive [{: x : y : direction} distance]
|
||||||
(match direction
|
(let [distance
|
||||||
:n {:y (+ y distance)}
|
(if (. {:w true :e true} direction)
|
||||||
:s {:y (- y distance)}
|
(/ distance (math.cos (/ (* math.pi y) 180)))
|
||||||
:w {:x (- x distance)}
|
distance)]
|
||||||
:e {:x (+ x 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]
|
(fn turn-left [{: x : y : direction}]
|
||||||
{:direction
|
(if (= y 90)
|
||||||
(-> {
|
{:x (wrap-longitude (- x 90))}
|
||||||
:n :w
|
{:direction
|
||||||
:w :s
|
(-> {
|
||||||
:s :e
|
:n :w
|
||||||
:e :n
|
:w :s
|
||||||
}
|
:s :e
|
||||||
(. direction))})
|
:e :n
|
||||||
|
}
|
||||||
|
(. direction))}))
|
||||||
|
|
||||||
(fn turn-right [direction]
|
(fn turn-right [{: x : y : direction}]
|
||||||
{:direction
|
(if (= y 90)
|
||||||
(-> {
|
{:x (wrap-longitude (+ x 90))}
|
||||||
:n :e
|
{:direction
|
||||||
:e :s
|
(-> {
|
||||||
:s :w
|
:n :e
|
||||||
:w :n
|
:e :s
|
||||||
}
|
:s :w
|
||||||
(. direction))})
|
:w :n
|
||||||
|
}
|
||||||
|
(. direction))}))
|
||||||
|
|
||||||
(fn command [rover string]
|
(fn command [rover string]
|
||||||
(merge rover
|
(merge rover
|
||||||
(match string
|
(match string
|
||||||
:f (drive rover 1)
|
:f (drive rover 1)
|
||||||
:b (drive rover -1)
|
:b (drive rover -1)
|
||||||
:r (turn-right rover.direction)
|
:r (turn-right rover)
|
||||||
:l (turn-left rover.direction)
|
:l (turn-left rover)
|
||||||
_ (assert false (. "unrecognised command " string)))))
|
_ (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]
|
(fn execute [rover cmds]
|
||||||
(accumulate [rover rover
|
(accumulate [rover rover
|
||||||
_ c (ipairs cmds)]
|
_ c (ipairs cmds)]
|
||||||
(merge rover (command rover c))))
|
(merge rover (fudge-for-pole (command rover c)))))
|
||||||
|
|
||||||
(fn rover [x y direction]
|
(fn rover [x y direction]
|
||||||
{: x
|
{: x
|
||||||
|
@ -96,9 +116,9 @@
|
||||||
(expect "Moves north when pointing south and asked to move backward"
|
(expect "Moves north when pointing south and asked to move backward"
|
||||||
(execute r [:b]) {:x 3 :y 3 :direction :s}))
|
(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"
|
(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)]
|
(let [r (rover 2 4 :e)]
|
||||||
(expect "Rotates to south when pointing east and asked to turn right"
|
(expect "Rotates to south when pointing east and asked to turn right"
|
||||||
|
@ -114,8 +134,52 @@
|
||||||
|
|
||||||
(let [r (rover 1 1 :s)]
|
(let [r (rover 1 1 :s)]
|
||||||
(expect "Multiple commands are executed in the right order"
|
(expect "Multiple commands are executed in the right order"
|
||||||
(execute r [:l :l :f :f :f :r :f])
|
(execute r [:l :l :f :f :f :r :f :b])
|
||||||
{:x 2 :y 4 :direction :e}))
|
{: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")
|
(print "OK")
|
||||||
|
|
Loading…
Reference in New Issue