-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.js
43 lines (37 loc) · 1.09 KB
/
day6.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
35
36
37
38
39
40
41
42
43
import { readFile, writeAnswer } from './helpers.js';
function isUnique(str) {
return str.length === new Set(str).size;
}
function findStartOfPacketMarker(str, unique=4) {
for (let i=unique; i<str.length; i++) {
if (isUnique(str.substring(i-unique, i))) {
return i;
}
}
}
function solveTestProblems() {
const tests = [
'mjqjpqmgbljsphdztnvjfqwrcgsmlb',
'bvwbjplbgvbhsrlpgdmjqwftvncz',
'nppdvjthqldpwncqszvftbrmjlhg',
'nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg',
'zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw'
];
tests.forEach((input) => {
writeAnswer(findStartOfPacketMarker(input, 14), `Test ${input}`);
})
}
function solveProblem1(filename) {
const input = readFile(filename);
const answer = findStartOfPacketMarker(input);
writeAnswer(answer);
}
function solveProblem2(filename) {
const input = readFile(filename);
const answer = findStartOfPacketMarker(input, 14);
writeAnswer(answer, 2);
}
const filename = './day6.puzzle.txt';
solveTestProblems();
solveProblem1(filename);
solveProblem2(filename);