Open
Description
A function takes a position
{:dir :NORTH :x 0 :y 0}
and a move instruction
"R10"
(turn Right, go 10 units)returns a new position
{:direction EAST :forward 10 :back 0}`
"Given a position (direction, forward, back) and move instruction, returns a new position"
[position action]
(apply-delta (assoc position :direction (turn position action)) action))
Now start with a single position and a sequence of directions, ["R10", "L20", "R10"] and return the final position.
pseudocode:
var pos = {:dir :NORTH :x 0 :y 0}
for (var dir : directions) {
pos = move(pos, dir)
}
return pos
In Clojure
`(reduce update-position initial-position)`
To see the "path" taken, use `reductions` instead
`(reductions update-position initial-position)`