-
Notifications
You must be signed in to change notification settings - Fork 0
/
14b.js
212 lines (173 loc) · 5.85 KB
/
14b.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
let knotHash = (lengthInput, size) => {
let strToList = (input) => input.split(',').map(i => parseInt(i)).filter(i => !isNaN(i));
let input = new Array(size).fill(0).map((v,i) => i);
let getLengths = (lengthInput) => {
return lengthInput.split('').map(l => {
let c = l.toString().charCodeAt(0);
if (c > 255) throw new Error('err');
return c;
}).concat([17, 31, 73, 47, 23]);
};
// to ascii
let lengths = getLengths(lengthInput);
if (JSON.stringify(getLengths(`1,2,3`)) !== JSON.stringify([49,44,50,44,51,17,31,73,47,23]))
throw new Error('getLengths error');
let slice = (list, startAt, count) => {
if (!Array.isArray(list)) throw new Error('not an array');
if (!Number.isInteger(startAt))
throw new Error(`startAt not a number`);
if (!Number.isInteger(count))
throw new Error(`count not a number`);
let result = [];
for (var i = startAt; i < startAt + count; i++) {
let ii = i;
while (ii >= list.length)
ii-=list.length;
let d = list[ii];
if (!Number.isInteger(d))
throw new Error(`d is not a number`);
result.push(d);
}
return result;
};
let reverseList = (list, startAt, count) => {
let sel = slice(list, startAt, count).reverse();
let a = 0;
let newList = list.slice();
for (var i = 0; i < sel.length; i++) {
if (startAt + i + a >= newList.length)
a = - i - startAt;
if (!Number.isInteger(sel[i]))
throw new Error(`not number`);
if (startAt + i + a >= newList.length)
throw new Error('overflow');
newList[startAt + i + a] = sel[i];
}
if (newList.length !== list.length)
throw new Error(`not same size lists`);
return newList;
}
let skipSize = 0;
let index = 0;
let hashRound = (input, lengths) => {
for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
let output = reverseList(input, index, lengths[lengthIndex]);
input = output;
index += lengths[lengthIndex] + skipSize;
while (index >= input.length)
index -= input.length;
skipSize++;
}
return input;
}
let test = hashRound(new Array(256).fill(0).map((v,i) => i), strToList(`147,37,249,1,31,2,226,0,161,71,254,243,183,255,30,70`));
if (test[0] * test[1] !== 37230)
throw new Error('hashRound invalid');
index = 0;
skipSize = 0;
for (let i = 0; i < 64; i++) {
let output = hashRound(input, lengths);
input = output;
}
let hash = '';
let n = 0;
for (var i = 0; i < 16; i++) {
let denseByte = 0;
for (var j = 0; j < 16; j++) {
denseByte = denseByte ^ input[i*16+j];
n++;
}
hash += denseByte.toString(16).padStart(2, '0');
}
if (n !== 256) throw new Error(`n !?`);
if (hash.length !== 2*16) throw new Error('inc length');
return hash;
}
function bitCount (n) {
n = n - ((n >> 1) & 0x55555555)
n = (n & 0x33333333) + ((n >> 2) & 0x33333333)
return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24
}
class Grid {
constructor() {
this.grid = [];
this.groups = 0;
}
add(rowNumber, byteNumber, byte) {
this.grid[rowNumber] = this.grid[rowNumber] || [];
this.grid[rowNumber] = this.grid[rowNumber].concat(byte.toString(2).padStart(8, '0').split(''));
}
isOutOfBounds(pos) {
let outOfBounds = pos.y >= this.grid.length ||
pos.y < 0 ||
pos.x >= this.grid[pos.y].length ||
pos.x < 0;
return outOfBounds;
}
isPosWalkable(pos) {
return !this.isOutOfBounds(pos) && this.grid[pos.y][pos.x] === '1';
}
getGroups() {
let pos;
let trail = [];
while ((pos = this.getNextUsedPos())) {
this.groups++;
trail.push(pos);
pos = { ...pos };
let backTracking = false;
do {
if ((!backTracking && !this.isPosWalkable(pos)) || pos.dir > 4) {
trail.pop();
pos = trail[trail.length - 1];
if (pos) {
pos.dir++;
}
backTracking = true;
continue;
}
backTracking = false;
this.grid[pos.y][pos.x] = '-';
if (!pos)
continue;
pos = { ...pos };
switch(pos.dir) {
case 1: pos.x++; break;
case 2: pos.y++; break;
case 3: pos.x--; break;
case 4: pos.y--; break;
default:
throw new Error('asdf');
}
pos.dir = 1;
trail.push(pos);
pos = { ...pos };
} while(trail.length);
}
return this.groups;
}
getNextY() {
for (let y = 0; y < this.grid.length; y++)
if (this.grid[y].indexOf('1') > -1)
return y;
}
getNextUsedPos() {
let y = this.getNextY();
if (y === -1 || y === undefined)
return;
let x = this.grid[y].indexOf('1');
return {x, y, dir: 1};
}
}
let input = `jzgqcdpd`;
let sum = 0;
let grid = new Grid();
for (let i = 0; i < 128; i++) {
let hash = knotHash(input + '-' + i, 256)
let pos = 0;
let parts = hash.match(/.{2}/g);
parts.map(part => parseInt(part, 16))
.map(byte => {
grid.add(i, pos++, byte);
});
}
console.log(grid.getGroups());