-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenge-15.ts
35 lines (26 loc) · 915 Bytes
/
challenge-15.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export function autonomousDrive(store: string[], movements: string[]) {
let row = store.findIndex(line => line.includes('!'))
let col = store[row].indexOf('!')
function placeRobot(robot: string) {
const line = store[row]
store[row] = line.slice(0, col) + robot + line.slice(col + 1)
}
placeRobot('.')
const maxRow = store.length
const maxCol = store[0].length
for (const move of movements) {
const rowMove = row + +(move === 'D') - +(move === 'U')
const colMove = col + +(move === 'R') - +(move === 'L')
const rowValues = [row, row, rowMove]
const colValues = [col, col, colMove]
const nextRow = rowValues[+(rowMove >= 0) + +(rowMove < maxRow)]
const nextCol = colValues[+(colMove >= 0) + +(colMove < maxCol)]
const pos = store[nextRow][nextCol]
if (pos !== '*') {
col = nextCol
row = nextRow
}
}
placeRobot('!')
return store
}