-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.js
49 lines (42 loc) · 920 Bytes
/
Day9.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
fs = require('fs');
fs.readFile('Day9.txt', 'utf8', function(err, data) {
if(err) {
return console.log(err);
}
let text = data;
let chars = text.split('');
chars = chars.splice(0, chars.length-1);
for(var i = 0; i < chars.length; i++) {
if(chars[i] === '!') {
chars.splice(i, 2);
i--;
}
}
let charsSubset = new Array();
let openGarbage = false;
let length = chars.length;
let garbageTot = 0;
for (var i = 0; i < length; i++) {
if (openGarbage && chars[i] === '>') {
openGarbage = false;
}
else if (openGarbage || chars[i] === '<') {
if(openGarbage) garbageTot++;
openGarbage = true;
}
else charsSubset.push(chars[i]);
}
let count = 0;
let level = 0;
charsSubset.forEach(function(char) {
if(char === '{') {
level++;
}
if(char === '}') {
count += level;
level--;
}
});
console.log("Answer 1: " + count);
console.log("Answer 2: " + garbageTot);
});