-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsix.js
More file actions
39 lines (33 loc) · 751 Bytes
/
Copy pathsix.js
File metadata and controls
39 lines (33 loc) · 751 Bytes
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
const { readFile } = require('fs/promises');
// 6
async function init() {
const input = await readFile('six.txt', 'utf8');
// const input = await readFile('six.test.txt', 'utf8');
const n = 14;
const pairs = genPairs(n);
const len = scan(input, n, pairs);
console.log(len + 1);
}
function genPairs(n) {
const pairs = [];
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
pairs.push([i, j]);
}
}
return pairs;
}
function scan(input, n, pairs) {
for (let i = n - 1; i < input.length; i++) {
let matches = 0;
pairs.forEach((pair) => {
if (input[i - pair[0]] === input[i - pair[1]]) {
matches++;
}
});
if (matches === 0) {
return i;
}
}
}
init();