39 lines
947 B
JavaScript
39 lines
947 B
JavaScript
|
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);
|