-
Notifications
You must be signed in to change notification settings - Fork 0
/
22.js
37 lines (30 loc) · 986 Bytes
/
22.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
const readline = require("readline");
const fs = require("fs");
const path = require("path");
// make a language that will suit the problem
String.prototype.charValues = function cv() {
return this.split("").map((char) => {
const charCodeStarting = 64;
const charCode = char.toUpperCase().charCodeAt(0);
return charCode - charCodeStarting;
});
};
Array.prototype.sum = function sm() {
return this.reduce((acc, n) => Number(n) + acc, 0);
};
const filePath = path.join(__dirname, "p022_names_sorted.txt");
const fileReadStream = readline.createInterface({
input: fs.createReadStream(filePath),
});
let lineCount = 1;
let nameScores = 0;
fileReadStream.on("line", (line) => {
const name = line.trim().replace(/"/gim, "").replace(/,/, "");
const scoreBeforeLineProduct = name.charValues().sum();
const score = scoreBeforeLineProduct * lineCount;
nameScores += score;
lineCount += 1;
});
fileReadStream.on("close", () => {
console.log(nameScores);
});