-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.ts
39 lines (29 loc) · 770 Bytes
/
6.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
33
34
35
36
37
38
39
import { readFile } from 'fs/promises'
const input = (await readFile('input.txt')).toString().trim()
// part 1
const uniqueChars: string[] = []
for (const [index, char] of input.split('').entries()) {
if (!uniqueChars.includes(char)) {
uniqueChars.push(char)
if (uniqueChars.length === 4) {
console.log(index + 1)
break
}
} else {
uniqueChars.splice(0, uniqueChars.length)
}
}
// part 2
const uniqueChars2: string[] = []
for (const [index, char] of input.split('').entries()) {
if (!uniqueChars2.includes(char)) {
uniqueChars2.push(char)
if (uniqueChars2.length === 13) {
console.log(index + 1)
break
}
} else {
uniqueChars2.push(char)
uniqueChars2.splice(0, uniqueChars2.length - 1)
}
}