-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifteen.js
More file actions
120 lines (96 loc) · 2.4 KB
/
Copy pathfifteen.js
File metadata and controls
120 lines (96 loc) · 2.4 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { readFile } from 'fs/promises';
let MAP = [];
let BEST;
let BEST2;
// 15a
async function init() {
console.time('0');
const input = await readFile('fifteen.txt', 'utf8');
const piece = input
.split('\n')
.map((line) => line.split('').map((d) => parseInt(d)));
buildMap(piece);
BEST = MAP.map((line) => line.map(() => -1));
console.timeEnd('0');
console.time('a');
console.time('b');
// kick it off with right and down
BEST[0][0] = 0;
tryNext1({ x: 0, y: 0 }, 1, 0);
tryNext1({ x: 0, y: 0 }, 0, 1);
console.log(BEST[BEST.length - 1][BEST[0].length - 1]);
console.timeEnd('a');
// re-run it and allow zigzag
BEST2 = MAP.map((line) => line.map(() => -1));
BEST2[0][0] = 0;
tryNext2({ x: 0, y: 0 }, 1, 0);
tryNext2({ x: 0, y: 0 }, 0, 1);
console.log(BEST2[BEST.length - 1][BEST[0].length - 1]);
console.timeEnd('b');
}
function buildMap(piece) {
const length = piece.length;
for (let i = 0; i < 5; i++) {
for (let x = 0; x < length; x++) {
const xx = i * length + x;
MAP[xx] = [];
for (let j = 0; j < 5; j++) {
for (let y = 0; y < length; y++) {
const yy = j * length + y;
let val = piece[x][y] + i + j;
if (val > 9) {
val -= 9;
}
if (val > 9) {
val -= 9;
}
MAP[xx][yy] = val;
}
}
}
}
}
function tryNext1(currentNode, a, b) {
const { x, y } = currentNode;
const lastScore = BEST[x][y];
const nextScore = lastScore + MAP[a][b];
if (BEST[a][b] === -1 || BEST[a][b] > nextScore) {
BEST[a][b] = nextScore;
// right
if (a + 1 < MAP.length) {
tryNext1({ x: a, y: b }, a + 1, b);
}
// down
if (b + 1 < MAP[0].length) {
tryNext1({ x: a, y: b }, a, b + 1);
}
}
}
function tryNext2(currentNode, a, b) {
const { x, y } = currentNode;
const lastScore = BEST2[x][y];
const nextScore = lastScore + MAP[a][b];
if (
(BEST2[a][b] === -1 && nextScore <= BEST[a][b]) ||
BEST2[a][b] > nextScore
) {
BEST2[a][b] = nextScore;
// up
if (b - 1 >= 0) {
tryNext2({ x: a, y: b }, a, b - 1);
}
// left
if (a - 1 >= 0) {
tryNext2({ x: a, y: b }, a - 1, b);
}
// right
if (a + 1 < MAP.length) {
tryNext2({ x: a, y: b }, a + 1, b);
}
// down
if (b + 1 < MAP[0].length) {
tryNext2({ x: a, y: b }, a, b + 1);
}
}
}
init();