In this challenge, the work area will be a 4x4 matrix containing three types of characters:
@ Your position.- A blank spot.&, #, $ Collectible items.From your starting point in the grid, you have to trace a route placing a x in every spot visited, following a sequence of letters with the directions to take:
l Leftr Rightu Upd Downinput = "luur"
// left, up, up, right
grid = [
["-", "-", "-", "#"]
["-", "-", "$", "-"]
["-", "-", "-", "@"]
["-", "&", "-", "-"] ]
result = { route = [
["-", "-", "x", "x"],
["-", "-", "x", "-"],
["-", "-", "x", "@"],
["-", "&", "-", "-"] ],
items: "$#" }
When moves are finished, you will have a new grid with the traced given route (a route made of x chars), and a series of items collected along the way.
Given a matrix grid and a string with the directions to take input, implement a function that returns an object literal containing the traced route as a new matrix and the items collected as a string.
function routeTracer([
["-", "#", "-", "-"],
["$", "#", "@", "&"],
["-", "#", "$", "&"],
["$", "-", "&", "-"]
], "lddr",) ➞ {
route: [
["-", "#", "-", "-"],
["$", "x", "@", "&"],
["-", "x", "$", "&"],
["$", "x", "x", "-"]
],
items: "##&"
}
x will overwite the symbol already present in the spot visited.