-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.js
34 lines (26 loc) · 997 Bytes
/
06.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
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {readInput} from '../../../util.js';
const directoryPath = path.dirname(fileURLToPath(import.meta.url));
const input = await readInput(directoryPath);
function findDistinctCharactersPosition(numberOfDistinctCharacters) {
let distinctCharacters = [];
let position = 0;
// eslint-disable-next-line unicorn/no-for-loop
for (let index = 0; index < input.length; index++) {
const currentCharacter = input[index];
if (distinctCharacters.includes(currentCharacter)) {
distinctCharacters = distinctCharacters.slice(
distinctCharacters.indexOf(currentCharacter) + 1,
);
}
distinctCharacters.push(currentCharacter);
if (distinctCharacters.length === numberOfDistinctCharacters) {
position = index + 1;
break;
}
}
return position;
}
console.log('Start of packet position:', findDistinctCharactersPosition(4));
console.log('Start of message position:', findDistinctCharactersPosition(14));