-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09-2.js
51 lines (45 loc) · 1.33 KB
/
day09-2.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
51
"use strict";
const io = require('./io.js');
function decompressedLength(input) {
let length = 0;
let inMarker = false;
let characterCount = 0;
let section = {repeat:0, len:0};
let buffer = "";
for (let c of input) {
if (inMarker) {
if (c == 'x') {
section.len = Number.parseInt(buffer);
buffer = "";
} else if (c == ')') {
section.repeat = Number.parseInt(buffer);
inMarker = false;
characterCount = section.len;
buffer = "";
} else {
buffer += c;
}
} else if (characterCount > 0) {
buffer += c;
characterCount--;
if (characterCount == 0) {
length += decompressedLength(buffer) * section.repeat;
section = {repeat:0, len:0};
}
} else if (c == '(') {
inMarker = true;
buffer="";
} else {
length++;
}
}
return length;
}
console.log(decompressedLength("(3x3)XYZ"));
console.log(decompressedLength("X(8x2)(3x3)ABCY"));
console.log(decompressedLength("(27x12)(20x12)(13x14)(7x10)(1x12)A"));
io.readFromFile('day09.txt')
.then((lines) => {
let result = decompressedLength(lines[0]);
console.log(result);
});