diff --git a/rover.fnl b/rover.fnl index 869da9f..436541c 100644 --- a/rover.fnl +++ b/rover.fnl @@ -13,6 +13,13 @@ :actual actual# }))))) +(fn drive [{: x : y : direction} distance] + (match direction + :n {:y (+ y distance)} + :s {:y (- y distance)} + :w {:x (- x distance)} + :e {:x (+ x distance)})) + (fn turn-left [direction] (-> { :n :w @@ -34,16 +41,8 @@ (fn command [rover string] (merge rover (match string - :f (match rover.direction - :n {:y (+ rover.y 1)} - :s {:y (- rover.y 1)} - :w {:x (- rover.x 1)} - :e {:x (+ rover.x 1)}) - :b (match rover.direction - :n {:y (- rover.y 1)} - :s {:y (+ rover.y 1)} - :w {:x (+ rover.x 1)} - :e {:x (- rover.x 1)}) + :f (drive rover 1) + :b (drive rover -1) :r {:direction (turn-right rover.direction)} :l {:direction (turn-left rover.direction)} _ (assert false (. "unrecognised command " string)))))