You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.0 KiB
JavaScript

4 years ago
const fs = require("fs");
exports.readInput = function (filename = "input") {
const input = fs.readFileSync(filename, "utf-8").trim();
const lines = input.split("\n");
const paragraphs = input.split("\n\n");
let colLength = lines[0].length;
let colConsistentcy = true;
for (const line of lines) {
if (colLength != line.length) {
colConsistentcy = false;
}
}
const avgLength = sum(lines.map((l) => l.length)) / lines.length;
console.log("Number of lines:", lines.length);
console.log("Number of paragraphs", paragraphs.length);
console.log("Col-length is consistent:", colConsistentcy);
console.log("Average col-length", avgLength);
console.log("Sample:");
for (const line of lines.slice(0, 5)) {
console.log(line);
}
console.log("...");
for (const line of lines.slice(lines.length - 5, lines.length)) {
console.log(line);
}
console.log("=".repeat(process.stdout.columns));
return {
raw: input,
lines,
paragraphs,
};
};
const sum = (arr) => arr.reduce((a, b) => a + b, 0);
exports.sum = sum;