-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15a.js
78 lines (61 loc) · 1.56 KB
/
15a.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {iterGrid, logGrid, readInput} from "./lib.js";
const input = readInput(15);
const brk = input.findIndex((line) => line === '');
const warehouse = input.slice(0, brk).map((line) => line.split(''));
const instructions = input.slice(brk + 1).map((line) => line.split('')).flat();
const robot = {
x: 0,
y: 0,
};
for (const [y, line] of warehouse.entries()) {
for (const [x, cell] of line.entries()) {
if (cell === '@') {
robot.x = x;
robot.y = y;
break;
}
}
}
const movements = {
'^': [0, -1],
'>': [1, 0],
'v': [0, 1],
'<': [-1, 0],
}
for (const instruction of instructions) {
const direction = movements[instruction];
if (move(warehouse, robot.x, robot.y, instruction)) {
robot.x += direction[0];
robot.y += direction[1];
}
}
function move(input, x, y, instruction) {
const delta = movements[instruction];
const cell = read(input, x, y, 0, 0);
if (cell === '#') {
return false;
}
if (cell === '.') {
return true;
}
if (cell === '@' || cell === 'O') {
const canMove = move(input, x + delta[0], y + delta[1], instruction);
if (canMove) {
input[y + delta[1]][x + delta[0]] = cell;
input[y][x] = '.';
}
return canMove;
}
}
function read(grid, x, y, dx, dy) {
return grid[y + dy]?.[x + dx];
}
logGrid(warehouse);
let gps = 0;
iterGrid(warehouse, (cell, x, y) => {
if (cell !== 'O') {
return;
}
gps += y * 100 + x;
});
console.log(gps);