separate turn-left and turn-right functions

main
Daniel Barlow 2023-01-22 18:09:39 +00:00
parent e746bb514e
commit 94325e53a0
1 changed files with 19 additions and 18 deletions

View File

@ -13,22 +13,23 @@
:actual actual# :actual actual#
}))))) })))))
(fn rotate [direction rotation] (fn turn-left [direction]
(let [chooser (-> {
(match rotation :n :w
:r { :w :s
:n :e :s :e
:e :s :e :n
:s :w }
:w :n (. direction)))
}
:l { (fn turn-right [direction]
:n :w (-> {
:w :s :n :e
:s :e :e :s
:e :n :s :w
})] :w :n
(. chooser direction))) }
(. direction)))
(fn command [rover string] (fn command [rover string]
@ -38,8 +39,8 @@
:s (merge rover {:y (- rover.y 1)}) :s (merge rover {:y (- rover.y 1)})
:w (merge rover {:x (- rover.x 1)}) :w (merge rover {:x (- rover.x 1)})
:e (merge rover {:x (+ rover.x 1)})) :e (merge rover {:x (+ rover.x 1)}))
:r (merge rover {:direction (rotate rover.direction :r)}) :r (merge rover {:direction (turn-right rover.direction)})
:l (merge rover {:direction (rotate rover.direction :l)}) :l (merge rover {:direction (turn-left rover.direction)})
_ (assert false (. "unrecognised command " string)))) _ (assert false (. "unrecognised command " string))))
(fn execute [rover [cmd & cmds]] (fn execute [rover [cmd & cmds]]