39 lines
947 B
JavaScript
Raw Normal View History

2024-12-02 10:50:22 +01:00
const fs = require("fs");
function parseInputAndCalculateSimilarity(filePath) {
const fileContent = fs.readFileSync(filePath, "utf-8");
const leftList = [];
const rightList = [];
fileContent.split("\n").forEach((line) => {
if (line.trim() !== "") {
const [left, right] = line.split(/\s+/).map(Number);
leftList.push(left);
rightList.push(right);
}
});
function calculateSimilarityScore(left, right) {
const rightCountMap = {};
right.forEach((num) => {
rightCountMap[num] = (rightCountMap[num] || 0) + 1;
});
let similarityScore = 0;
left.forEach((num) => {
if (rightCountMap[num]) {
similarityScore += num * rightCountMap[num];
}
});
return similarityScore;
}
return calculateSimilarityScore(leftList, rightList);
}
const filePath = "inputs/1";
const similarityScore = parseInputAndCalculateSimilarity(filePath);
console.log(similarityScore);