From 13e283a2a592eba668773c4d58254076be7b41f0 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 22 Jan 2023 18:47:54 +0000 Subject: [PATCH] extract `drive` function which can go forward and backward --- rover.fnl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) 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)))))