Initial commit

This commit is contained in:
jzitnik-dev 2024-12-02 10:50:22 +01:00
commit f0405677f3
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
7 changed files with 2170 additions and 0 deletions

38
1-2.js Normal file
View 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
View 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
View 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
View 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)

1000
inputs/1 Normal file

File diff suppressed because it is too large Load Diff

1000
inputs/2 Normal file

File diff suppressed because it is too large Load Diff

13
package.json Normal file
View 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": ""
}