Initial commit
This commit is contained in:
commit
f0405677f3
38
1-2.js
Normal file
38
1-2.js
Normal file
@ -0,0 +1,38 @@
|
||||
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);
|
34
1.js
Normal file
34
1.js
Normal file
@ -0,0 +1,34 @@
|
||||
const fs = require("fs");
|
||||
|
||||
function parseInputAndCalculateDistance(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 calculateTotalDistance(left, right) {
|
||||
left.sort((a, b) => a - b);
|
||||
right.sort((a, b) => a - b);
|
||||
|
||||
let totalDistance = 0;
|
||||
for (let i = 0; i < left.length; i++) {
|
||||
totalDistance += Math.abs(left[i] - right[i]);
|
||||
}
|
||||
|
||||
return totalDistance;
|
||||
}
|
||||
|
||||
return calculateTotalDistance(leftList, rightList);
|
||||
}
|
||||
|
||||
const filePath = "inputs/1";
|
||||
const totalDistance = parseInputAndCalculateDistance(filePath);
|
||||
|
||||
console.log(totalDistance);
|
50
2-2.js
Normal file
50
2-2.js
Normal file
@ -0,0 +1,50 @@
|
||||
const fs = require("fs");
|
||||
|
||||
const file = "inputs/2";
|
||||
const fileContent = fs.readFileSync(file, "utf-8").trim();
|
||||
|
||||
const reports = fileContent.split("\n");
|
||||
|
||||
let safeCount = 0;
|
||||
|
||||
function isSafe(numbers) {
|
||||
const isIncreasing = numbers.every(
|
||||
(num, i) => i === 0 || num > numbers[i - 1]
|
||||
);
|
||||
|
||||
const isDecreasing = numbers.every(
|
||||
(num, i) => i === 0 || num < numbers[i - 1]
|
||||
);
|
||||
|
||||
const isValidJumps = numbers.every(
|
||||
(num, i) => i === 0 || Math.abs(num - numbers[i - 1]) <= 3
|
||||
);
|
||||
|
||||
return (isIncreasing || isDecreasing) && isValidJumps;
|
||||
}
|
||||
|
||||
for (const element of reports) {
|
||||
const arr = element.trim().split(" ");
|
||||
const numbers = arr.map(Number);
|
||||
|
||||
if (isSafe(numbers)) {
|
||||
safeCount += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
let isSafeAfterRemoval = false;
|
||||
|
||||
for (let i = 0; i < numbers.length; i++) {
|
||||
const newNumbers = numbers.slice(0, i).concat(numbers.slice(i + 1));
|
||||
if (isSafe(newNumbers)) {
|
||||
isSafeAfterRemoval = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isSafeAfterRemoval) {
|
||||
safeCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(safeCount);
|
35
2.js
Normal file
35
2.js
Normal file
@ -0,0 +1,35 @@
|
||||
const fs = require("fs");
|
||||
|
||||
const file = "inputs/2";
|
||||
const fileContent = fs.readFileSync(file, "utf-8").trim();
|
||||
|
||||
const reports = fileContent.split("\n");
|
||||
|
||||
let safeCount = 0;
|
||||
|
||||
for (const element of reports) {
|
||||
const arr = element.trim().split(" ");
|
||||
const numbers = arr.map(Number);
|
||||
|
||||
const isIncreasing = numbers.every(
|
||||
(num, i) => i === 0 || num > numbers[i - 1],
|
||||
);
|
||||
|
||||
const isDecreasing = numbers.every(
|
||||
(num, i) => i === 0 || num < numbers[i - 1],
|
||||
);
|
||||
|
||||
if (!(isIncreasing || isDecreasing)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const isValidJumps = numbers.every(
|
||||
(num, i) => i === 0 || Math.abs(num - numbers[i - 1]) <= 3,
|
||||
);
|
||||
|
||||
if (isValidJumps) {
|
||||
safeCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(safeCount)
|
13
package.json
Normal file
13
package.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "adventofcode2024",
|
||||
"version": "1.0.0",
|
||||
"scripts": {
|
||||
"1": "node 1",
|
||||
"1-2": "node 1-2",
|
||||
"2": "node 2",
|
||||
"2-2": "node 2-2"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": ""
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user