This commit is contained in:
Aelita4 2022-12-06 17:14:03 +01:00
parent 65cfefb358
commit bf804c482c
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
1 changed files with 15 additions and 8 deletions

View File

@ -2,26 +2,33 @@ import { readFileSync } from "fs";
const input = readFileSync("datastream.txt", "utf-8"); const input = readFileSync("datastream.txt", "utf-8");
function checkForDuplicates(arr) { function checkForDuplicates(arr, howMany) {
const map = new Map(); const map = new Map();
arr.forEach(char => { arr.forEach(char => {
map.set(char, true); map.set(char, true);
}); });
return map.size === 4; return map.size === howMany;
} }
const arr = ['', '', '', '']; const arr1 = new Array(4).fill('');
let i = 1; const arr2 = new Array(14).fill('');
let i = 1, startOfPacket = 0, startOfMessage = 0;
console.log(arr1)
for(const char of input) { for(const char of input) {
arr.shift() arr1.shift()
arr.push(char); arr1.push(char);
if(i > 4 && checkForDuplicates(arr)) break; arr2.shift()
arr2.push(char);
if(startOfPacket === 0 && i > 4 && checkForDuplicates(arr1, 4)) startOfPacket = i;
if(startOfMessage === 0 && i > 14 && checkForDuplicates(arr2, 14)) startOfMessage = i;
i++; i++;
} }
console.log(i); console.log(startOfPacket, startOfMessage);