-
Notifications
You must be signed in to change notification settings - Fork 0
/
3b.js
74 lines (59 loc) · 1.59 KB
/
3b.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
class Map2 {
constructor() {
this.map = {};
}
set(x, y, value) {
this.map[x + ':' + y] = value;
}
get(x, y) {
return this.map[x + ':' + y] || 0;
}
getAdjescent(x, y) {
return this.get(x+1,y) +
this.get(x+1, y+1) +
this.get(x, y+1) +
this.get(x-1, y+1) +
this.get(x-1, y) +
this.get(x-1, y-1) +
this.get(x, y-1) +
this.get(x+1, y-1);
}
getNextSpacePosition(x,y) {
let tests = [
[0, 1],
[-1, 0],
[0, -1],
[1, 0]
];
let dir = tests.map(t => {
let nx = x + t[0];
let ny = y + t[1];
if (this.get(nx, ny)>0)
return null;
return {
x: nx,
y: ny,
d: Math.sqrt(nx*nx + ny*ny),
e: t[0] === 1 && t[1] === 0 ? .1 : 0
}
})
.filter(t => Boolean(t))
.sort((a, b) => a.d*100 + a.e - b.d*100 + b.e)[0];
return dir;
}
}
let map = new Map2();
let d = [0,1];
map.set(0,0,1);
let x = 1;
let y = 0;
for (let i = 0; i < 100; i++) {
map.set(x,y,map.getAdjescent(x,y));
if (map.get(x,y) > 368078) {
console.log(map.get(x,y));
break;
}
let m = map.getNextSpacePosition(x,y);
x = m.x;
y = m.y;
}