-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03_part2.js
More file actions
40 lines (33 loc) · 975 Bytes
/
day03_part2.js
File metadata and controls
40 lines (33 loc) · 975 Bytes
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
var fs = require('fs');
var path = require('path');
var filePath = './inputDay03.txt';
const caloriesSums = new Array();
let priority = {};
let p = 1;
for (let i = 97; i <= 122; ++i, ++p) {
priority[String.fromCharCode(i)] = p;
}
for (let i = 65; i <= 90; ++i, ++p) {
priority[String.fromCharCode(i)] = p;
}
let buffer = fs.readFileSync(path.join(__dirname, filePath));
let lines = buffer.toString().split('\n');
let totalPoints = 0;
for (let i = 0; i < lines.length; ++i) {
if (lines[i].length > 0 && (i % 3) == 0) {
let first = lines[i];
let second = lines[i + 1];
let third = lines[i + 2];
let id;
[...first].forEach((fc) => {
[...second].forEach((sc) => {
[...third].forEach((tc) => {
if (fc == sc && fc == tc)
id = fc;
});
});
});
totalPoints += priority[id];
}
};
console.log(totalPoints);