-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge15.js
More file actions
37 lines (31 loc) · 813 Bytes
/
challenge15.js
File metadata and controls
37 lines (31 loc) · 813 Bytes
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
36
37
/*
Challenge # 15
Title: ↔️ Autonomous robot
Level: Medium
Link: https://2023.adventjs.dev/en/challenges/2023/15
*/
function autonomousDrive(store, movements) {
let x = store.findIndex(row => row.includes('!'))
let y = store[x].indexOf('!')
const rightOrLeft = {
R: ['!.', '.!'],
L: ['.!', '!.']
}
const diretions = { R: 1, D: 1, L: -1, U: -1 }
for(const mov of movements) {
const n = diretions[mov]
if ('RL'.includes(mov)) {
if (store[x][y + n] == '.') {
store[x] = store[x].replace(...(rightOrLeft[mov]))
y += n
}
} else {
if (store[x + n]?.[y] == '.') {
store[x+n] = store[x+n].substr(0, y) + '!' + store[x+n].substr(y+1)
store[x] = store[x].replace('!', '.')
x += n
}
}
}
return store
}