handle north pole

main
Daniel Barlow 2023-01-22 22:05:14 +00:00
parent 4b1f2ee923
commit 64c9fd7b88
1 changed files with 47 additions and 26 deletions

View File

@ -29,39 +29,50 @@
: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}]
{: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
@ -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")