-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathten.js
More file actions
72 lines (60 loc) · 1.14 KB
/
Copy pathten.js
File metadata and controls
72 lines (60 loc) · 1.14 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { readFile } from 'fs/promises';
const CLOSERS = {
')': {
char: '(',
value: 3,
},
']': {
char: '[',
value: 57,
},
'}': {
char: '{',
value: 1197,
},
'>': {
char: '<',
value: 25137,
},
};
const LEFTOVER = {
'(': 1,
'[': 2,
'{': 3,
'<': 4,
};
async function init() {
console.time('a');
const input = await readFile('ten.txt', 'utf8');
const rows = input.split('\n');
let score = 0;
const scores = [];
rows.forEach((row) => {
const stack = [];
let skip = false;
for (let i = 0; i < row.length; i++) {
const d = CLOSERS[row[i]];
if (d) {
const top = stack.pop();
if (top !== d.char) {
score += d.value;
skip = true;
break;
}
} else {
stack.push(row[i]);
}
}
if (!skip && stack.length) {
const subScore = stack.reduceRight((acc, d) => {
return acc * 5 + LEFTOVER[d];
}, 0);
scores.push(subScore);
}
});
console.log({ score });
scores.sort((a, b) => a - b);
console.log({ b: scores[(scores.length - 1) / 2] });
console.timeEnd('a');
}
init();