-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.ts
32 lines (27 loc) · 772 Bytes
/
06.ts
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
import {loadData} from '../shared/utils';
export type DataType = string;
/* istanbul ignore next */
export const parse = async (): Promise<Array<DataType>> =>
(await loadData(2022, 6)).split('');
function findUniqueSubsequence(
sequence: Array<DataType>,
length: number
): number {
let marker: Array<string> = [];
for (let i = 0; i < sequence.length; i += 1) {
while (marker.includes(sequence[i])) {
marker.shift();
}
marker = [...marker, sequence[i]];
if (marker.length === length) {
return i + 1;
}
}
return -1;
}
export const part1 = (data: Array<DataType>): number => {
return findUniqueSubsequence(data, 4);
};
export const part2 = (data: Array<DataType>): number => {
return findUniqueSubsequence(data, 14);
};