-
Notifications
You must be signed in to change notification settings - Fork 0
/
3a.js
62 lines (46 loc) · 1.34 KB
/
3a.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
'use strict';
/*
w x h sum
1 1 1
3 3 8 (9 - 1)
5 5 16 (25 - 9)
*/
let findMoves = (searchIndex) => {
let level = 0;
let prevSize = 1;
for (let size = 3; size < 1050; size += 2) {
let min = prevSize + 1;
let max = size * size;
if (!(searchIndex >= min && searchIndex <= max))
continue;
let upper = parseInt(size / 2);
let move = (p, turn) => {
if (p.x === upper && p.y === upper)
turn = [-1, 0];
else if (p.x === -upper && p.y === upper)
turn = [0, -1];
else if (p.x === -upper && p.y === -upper)
turn = [1, 0];
else if (p.x === upper && p.y === -upper)
turn = [0, 1];
p.x += turn[0];
p.y += turn[1];
return { p, turn };
};
let calc = (i) => {
let p = { y: -upper + 1, x: upper };
let turn = [0,1];
while (i-- > 0) {
let result = move(p, turn);
p = result.p;
turn = result.turn;
}
return p;
}
let i = searchIndex - min;
let result = calc(i)
return Math.abs(result.x) + Math.abs(result.y);
}
}
//console.log(findMoves(1024))
console.log(findMoves(368078))