-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
61 lines (49 loc) · 1.21 KB
/
parser.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
58
59
60
61
const fs = require("fs");
const INPUT_FILE = process.argv[2];
const FILE_NAME = INPUT_FILE.substring(INPUT_FILE.lastIndexOf("\\") + 1);
function getData(fileName) {
const lines = fs
.readFileSync(fileName, "utf8")
.toString()
.split(String.fromCharCode(10));
const [_, __, dayCount] = lines[0].split(" ");
const bookScores = lines[1].split(" ").map(Number);
const libs = [];
for (i = 2; lines[i].length > 0; i += 2) {
const [___, signup, cap] = lines[i].split(" ").map(Number);
const books = lines[i + 1].split(" ");
const scoreSum = books.reduce(
(acc, bookId) => (acc += bookScores[bookId]),
0
);
libs.push({
signup,
cap,
books,
libId: i / 2 - 1,
scoreSum
});
}
return {
dayCount,
bookScores,
libs
};
}
function buildOutput(libs) {
const lines = [`${libs.length}`];
for (lib of libs) {
lines.push(`${lib.libId} ${lib.books.length}`);
lines.push(lib.books.join(" "));
}
fs.writeFile(
`./outputs/output-${FILE_NAME}`,
lines.join(String.fromCharCode(10)),
"utf8",
err => {
if (err) return console.log(err);
}
);
}
exports.getData = getData;
exports.buildOutput = buildOutput;