-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartTwo.js
52 lines (46 loc) · 1.06 KB
/
partTwo.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const fs = require('fs');
const instructions = fs
.readFileSync(`${__dirname}/input.txt`)
.toString()
.split('\n')
.map((line) => line.split(''));
const DIALS = [
[null, null, 1, null, null],
[null, 2, 3, 4, null],
[5, 6, 7, 8, 9],
[null, 'A', 'B', 'C', null],
[null, null, 'D', null, null]
];
const MAX_INDEX = DIALS.length - 1;
const pos = { x: 0, y: 2 };
let code = '';
instructions.forEach((line) => {
line.forEach((instruction) => { // NOSONAR
switch (instruction) {
case 'U':
if (pos.y > 0 && DIALS[pos.y - 1][pos.x]) {
pos.y--;
}
break;
case 'D':
if (pos.y < MAX_INDEX && DIALS[pos.y + 1][pos.x]) {
pos.y++;
}
break;
case 'L':
if (pos.x > 0 && DIALS[pos.y][pos.x - 1]) {
pos.x--;
}
break;
case 'R':
if (pos.x < MAX_INDEX && DIALS[pos.y][pos.x + 1]) {
pos.x++;
}
break;
default:
console.log('What is going on?');
}
});
code += DIALS[pos.y][pos.x];
});
console.log(code);