-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6.js
More file actions
43 lines (32 loc) · 980 Bytes
/
day6.js
File metadata and controls
43 lines (32 loc) · 980 Bytes
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 fs from 'fs'
const data = fs.readFileSync('../inputs/day6.txt').toString('utf-8')
// Short functional solution but slow
const findUniqueSubstringIndex = (str, wSize, i = 0) =>
i + wSize <= str.length
? new Set(str.slice(i, i + wSize)).size === wSize
? i + wSize
: findUniqueSubstringIndex(str, wSize, i + 1)
: -1
// Fast as fuck
const fUniqueStringsIndex = (str, wSize) => {
const charCounts = {}
let uniqueCount = 0
for (let i = 0; i < str.length; i++) {
const char = str[i]
charCounts[char] = (charCounts[char] ?? 0) + 1
if (charCounts[char] === 1) {
uniqueCount++
}
if (i >= wSize) {
const removedChar = str[i - wSize]
charCounts[removedChar]--
if (charCounts[removedChar] === 0) uniqueCount--
}
if (uniqueCount === wSize) return i + 1
}
return -1
}
const part1 = fUniqueStringsIndex(data, 4)
const part2 = fUniqueStringsIndex(data, 14)
console.log(part1)
console.log(part2)