handle north pole
This commit is contained in:
parent
4b1f2ee923
commit
64c9fd7b88
45
rover.fnl
45
rover.fnl
@ -29,7 +29,9 @@
|
|||||||
:w {:x (wrap-longitude (- x distance))}
|
:w {:x (wrap-longitude (- x distance))}
|
||||||
:e {:x (wrap-longitude (+ x distance))})))
|
:e {:x (wrap-longitude (+ x distance))})))
|
||||||
|
|
||||||
(fn turn-left [direction]
|
(fn turn-left [{: x : y : direction}]
|
||||||
|
(if (= y 90)
|
||||||
|
{:x (wrap-longitude (- x 90))}
|
||||||
{:direction
|
{:direction
|
||||||
(-> {
|
(-> {
|
||||||
:n :w
|
:n :w
|
||||||
@ -37,9 +39,11 @@
|
|||||||
:s :e
|
:s :e
|
||||||
:e :n
|
:e :n
|
||||||
}
|
}
|
||||||
(. direction))})
|
(. direction))}))
|
||||||
|
|
||||||
(fn turn-right [direction]
|
(fn turn-right [{: x : y : direction}]
|
||||||
|
(if (= y 90)
|
||||||
|
{:x (wrap-longitude (+ x 90))}
|
||||||
{:direction
|
{:direction
|
||||||
(-> {
|
(-> {
|
||||||
:n :e
|
:n :e
|
||||||
@ -47,21 +51,28 @@
|
|||||||
:s :w
|
:s :w
|
||||||
:w :n
|
:w :n
|
||||||
}
|
}
|
||||||
(. direction))})
|
(. 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
|
||||||
@ -155,10 +166,20 @@
|
|||||||
(execute (rover 179 0 :e) [:f :f :f])
|
(execute (rover 179 0 :e) [:f :f :f])
|
||||||
{:x -178 :y 0 :direction :e})
|
{:x -178 :y 0 :direction :e})
|
||||||
|
|
||||||
"if we travel west past x=-180, x becomes positive 180"
|
(expect "At the North Pole we always point south"
|
||||||
"if we travel east past x=180, x becomes -180"
|
(execute (rover 0 89 :n) [:f])
|
||||||
"if we travel east/west at a non-zero latitude, we don't move a full unit"
|
{:x 0 :y 90 :direction :s})
|
||||||
"if we arrive at the north pole, we are {:y 90 :direction :s}"
|
|
||||||
"if we arrive at the north pole and turn 90 degrees left, x = x - 90"
|
(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
Block a user