diff --git a/rover.fnl b/rover.fnl index f60fd06..fdff0ec 100644 --- a/rover.fnl +++ b/rover.fnl @@ -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")