33 lines
787 B
JavaScript
33 lines
787 B
JavaScript
|
import { readFileSync } from 'node:fs';
|
||
|
|
||
|
const input = readFileSync('input', 'utf-8');
|
||
|
|
||
|
function isValidMarker(marker) {
|
||
|
return marker.split('').every((c, i, word) => word.lastIndexOf(c) === i);
|
||
|
}
|
||
|
|
||
|
function findMarker(markerLength) {
|
||
|
for (let pos = markerLength; pos <= input.length; pos++) {
|
||
|
const marker = input.slice(pos - markerLength, pos);
|
||
|
if (isValidMarker(marker)) {
|
||
|
return { marker, pos };
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const packetMarker = findMarker(4);
|
||
|
if (packetMarker) {
|
||
|
console.log(
|
||
|
`Found packet marker: ${packetMarker.marker} after: ${packetMarker.pos} ` +
|
||
|
`characters`
|
||
|
);
|
||
|
}
|
||
|
|
||
|
const messageMarker = findMarker(14);
|
||
|
if (messageMarker) {
|
||
|
console.log(
|
||
|
`Found message marker: ${messageMarker.marker} after: ` +
|
||
|
`${messageMarker.pos} characters`
|
||
|
);
|
||
|
}
|