-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.js
50 lines (35 loc) · 1.08 KB
/
09.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
44
45
46
47
48
49
50
const DECOMPRESS_REGEX = /\((\d+)x(\d+)\)/
const part1 = (input) => {
const cleanInput = input.trim()
let totalChars = 0,
lastMatchedIndex = 0
const re = new RegExp(DECOMPRESS_REGEX, 'g')
let match
while ((match = re.exec(cleanInput)) !== null) {
const a = parseInt(match[1], 10)
const b = parseInt(match[2], 10)
re.lastIndex += a
totalChars += a * b + (match.index - lastMatchedIndex)
lastMatchedIndex = match.index + match[0].length + a
}
return totalChars + (cleanInput.length - lastMatchedIndex)
}
const part2 = (input) => {
let remaining = input.trim()
let totalChars = 0
let match
while ((match = DECOMPRESS_REGEX.exec(remaining)) !== null) {
const a = parseInt(match[1], 10)
const b = parseInt(match[2], 10)
const dataIndex = match.index + match[0].length
const dataEndIndex = dataIndex + a
const data = remaining.slice(dataIndex, dataEndIndex)
totalChars += part2(data) * b + match.index
remaining = remaining.slice(dataEndIndex)
}
return totalChars + remaining.length
}
module.exports = {
part1,
part2,
}