generated from mariotacke/template-advent-of-code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
part1.js
56 lines (44 loc) · 1.21 KB
/
part1.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
module.exports = (input) => {
const map = input
.split('\n')
.map((line) => line.trim().split('').map((p) => +p));
let lowestTotalRisk = Array
.from({ length: map.length })
.reduce((total, _, i) => total + map[i][map.length - 1] + map[0][i], 0);
const lowestPositionRisks = Array
.from({ length: map.length })
.map(() => Array
.from({ length: map.length })
.map(() => Infinity));
const walk = (cost, [px, py]) => {
if (cost >= lowestPositionRisks[py][px]) {
return;
}
lowestPositionRisks[py][px] = cost;
if (px === map.length - 1 && py === map.length - 1) {
if (cost < lowestTotalRisk) {
lowestTotalRisk = cost;
}
return;
}
const neighbors = [
[px + 1, py], // right
[px - 1, py], // left
[px, py + 1], // down
[px, py - 1], // up
];
for (const [nx, ny] of neighbors) {
// out of bounds
if (nx < 0 || nx >= map.length || ny < 0 || ny >= map.length) {
continue;
}
// cost too high
if (cost + map[ny][nx] >= lowestTotalRisk) {
continue;
}
walk(cost + map[ny][nx], [nx, ny]);
}
};
walk(0, [0, 0]);
return lowestTotalRisk;
};