implement rotate right

main
Daniel Barlow 2023-01-22 18:03:16 +00:00
parent f8a58c4a68
commit 78484b1115
1 changed files with 31 additions and 7 deletions

View File

@ -13,15 +13,31 @@
:actual actual#
})))))
(fn command [rover command]
(match rover.direction
:n (merge rover {:y (+ rover.y 1)})
:s (merge rover {:y (- rover.y 1)})
:w (merge rover {:x (- rover.x 1)})
:e (merge rover {:x (+ rover.x 1)})))
(fn rotate [direction rotation]
(let [chooser
(match rotation
:r {
:n :e
:e :s
:s :w
:w :n
}
)]
(. chooser direction)))
(fn command [rover string]
(match string
:f (match rover.direction
:n (merge rover {:y (+ rover.y 1)})
:s (merge rover {:y (- rover.y 1)})
:w (merge rover {:x (- rover.x 1)})
:e (merge rover {:x (+ rover.x 1)}))
:r (merge rover {:direction (rotate rover.direction :r)})
_ (assert false (. "unrecognised command " string))))
(fn execute [rover [cmd & cmds]]
(let [r1 (command rover)]
(let [r1 (command rover cmd)]
(if (next cmds)
(r1:execute cmds)
r1)))
@ -65,5 +81,13 @@
(r:execute [:f :f :f])
{:x 1 :y -2 :direction :s}))
(let [r (rover 2 4 :e)]
(expect "Rotates to south when pointing east and asked to turn right"
(r:execute [:r]) {:x 2 :y 4 :direction :s}))
(let [r (rover 2 4 :w)]
(expect "Rotates to north when pointing west and asked to turn right"
(r:execute [:r]) {:x 2 :y 4 :direction :n}))
(print "OK")