-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6.js
More file actions
63 lines (54 loc) · 1.67 KB
/
Copy pathday6.js
File metadata and controls
63 lines (54 loc) · 1.67 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import fs from 'fs';
const input = fs.readFileSync('inputs/day6.txt', 'utf8');
const sampleInput = 'mjqjpqmgbljsphdztnvjfqwrcgsmlb';
const findFirstFour = (input) => {
const inputArray = input.split('');
const firstFour = [];
const firstFourIndex = [];
let i = 0;
while (firstFour.length < 4) {
const currentChar = inputArray[i];
const next3Chars = inputArray.slice(i + 1, i + 4);
if (
!next3Chars.includes(currentChar) &&
!next3Chars.some((char, index) => next3Chars.indexOf(char) !== index)
) {
firstFour.push(currentChar);
firstFourIndex.push(i);
next3Chars.forEach((char, index) => {
firstFour.push(char);
firstFourIndex.push(i + index + 1);
});
}
i++;
}
console.log(firstFourIndex[firstFourIndex.length - 1] + 1);
return firstFour.join('');
};
console.log(findFirstFour(input));
const findFirstFourteen = (input) => {
const inputArray = input.split('');
const firstFourteen = [];
const firstFourteenIndex = [];
let i = 0;
while (firstFourteen.length < 14) {
const currentChar = inputArray[i];
const next13Chars = inputArray.slice(i + 1, i + 14);
if (
!next13Chars.includes(currentChar) &&
!next13Chars.some((char, index) => next13Chars.indexOf(char) !== index)
) {
firstFourteen.push(currentChar);
firstFourteenIndex.push(i);
next13Chars.forEach((char, index) => {
firstFourteen.push(char);
firstFourteenIndex.push(i + index + 1);
});
}
i++;
}
console.log(firstFourteen);
console.log(firstFourteenIndex[firstFourteenIndex.length - 1] + 1);
return firstFourteen.join('');
};
console.log(findFirstFourteen(input));