-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9a.js
57 lines (49 loc) · 1.29 KB
/
9a.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
52
53
54
55
56
57
/*jshint esversion: 6 */
let _ = require("lodash");
var fs = require('fs');
var stream = fs.readFileSync('./data/9-input.txt').toString();
console.log(`Read file, ${stream.length} characters found`);
let cleanStream = function (s) {
let garbage = false;
let correct = false;
let chars = s.split("");
return chars.reduce((clean, c) => {
if (garbage) {
if (correct) {
correct = false;
return clean;
}
if (c === '!') {
correct = true;
return clean;
}
if (c === '>') {
garbage = false;
return clean;
}
return clean;
}
if (c === '<') {
garbage = true;
return clean;
}
if (c === '}' || c === "{") {
return clean + c;
}
return clean;
}, "");
};
let cs = cleanStream(stream);
//console.log(cs);
let csArray = cs.split("");
let calcScore = function (array) {
var level = 1;
let score = array.reduce((score, c) => {
level = level + ((c === '{') ? 1 : -1);
return score + level * ((c === '{') ? 0 : 1);
}, 0);
return score;
};
let score = calcScore(csArray);
console.log(`score is ${score}`);
// 10050