Solve 2020/06

master
Alfred Melch 4 years ago
parent e02c91021c
commit 74c926ab5d

File diff suppressed because it is too large Load Diff

@ -0,0 +1,23 @@
const { readInput, sum } = require("./util");
const { paragraphs } = readInput();
function intersect(strA, strB) {
const setB = new Set(strB);
return strA
.split("")
.filter((el) => setB.has(el))
.join("");
}
function intersections(...strings) {
let res = strings[0];
for (const cur of strings.slice(1)) {
res = intersect(res, cur);
}
return res;
}
console.log("Part1:");
console.log(sum(paragraphs.map((p) => new Set(p.replaceAll("\n", "")).size)));
console.log("Part2:");
console.log(sum(paragraphs.map((p) => intersections(...p.split("\n")).length)));

@ -0,0 +1,41 @@
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;
Loading…
Cancel
Save