-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10a.js
36 lines (28 loc) · 859 Bytes
/
10a.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
import {iterGrid, readInput} from "./lib.js";
const input = readInput(10)
.map(line => line.split('').map(cell => parseInt(cell)));
const answer = [];
iterGrid(input, (cell, x, y) => {
if (cell === 0) {
answer.push(score(input, [], -1, x, y));
}
});
function score(grid, route, prevTile, x, y) {
const cell = grid[y]?.[x];
// console.log(cell);
if (cell === undefined || isNaN(cell) || cell - prevTile !== 1) {
return 0;
}
if (cell === 9) {
if (route.includes(`${x},${y}`)) {
return 0;
}
route.push(`${x},${y}`);
return 1;
}
return score(grid, route, cell, x, y + 1)
+ score(grid, route, cell, x, y - 1)
+ score(grid,route, cell, x + 1, y)
+ score(grid,route, cell, x - 1, y);
}
console.log(answer.reduce((a, b) => a + b, 0));