-
Notifications
You must be signed in to change notification settings - Fork 5
/
day06.mjs
41 lines (36 loc) · 1020 Bytes
/
day06.mjs
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
import { readFileSync } from "node:fs";
const input = readFileSync("day06.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.trim(); // Remove starting/ending whitespace
function isUnique(array) {
return new Set(array).size === array.length;
}
function part1() {
let slidingWindow = [];
for (let i = 0; i < input.length; i++) {
slidingWindow.push(input[i]);
if (slidingWindow.length > 4) {
slidingWindow.shift();
}
if (slidingWindow.length === 4 && isUnique(slidingWindow)) {
console.log(i + 1);
break;
}
}
}
function part2() {
const windowLength = 14;
let slidingWindow = [];
for (let i = 0; i < input.length; i++) {
slidingWindow.push(input[i]);
if (slidingWindow.length > windowLength) {
slidingWindow.shift();
}
if (slidingWindow.length === windowLength && isUnique(slidingWindow)) {
console.log(i + 1);
break;
}
}
}
part1();
part2();