spaghetti at a wall and none of its edible

This commit is contained in:
Skylar Grant 2025-12-04 22:39:32 -05:00
commit a0b4c83c44
5 changed files with 211 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
src/inputs*

42
src/01a.js Normal file
View File

@ -0,0 +1,42 @@
import data from "./inputs/01ex.json" with { type: "json" };
const inputs = data.inputs;
/* Global Variables */
let zeroCount = 0;
/* Starting Dial Position */
let dialPos = 50;
/* Functions */
/**
*
* @param {String} direction - "L" or "R"
* @param {Number} magnitude - Number of positions to rotate the dial
* @param {Number} begDialPos - dialPos before any movement
* @returns {Number} newDialPos
*/
function rotateDial(direction, magnitude, begDialPos) {
let newDialPos = begDialPos;
switch(direction) {
case 'L':
newDialPos -= magnitude % 100;
break;
case 'R':
newDialPos += magnitude % 100;
break;
default:
throw new Error("Invalid direction provided.");
}
if (newDialPos < 0) newDialPos += 100;
if (newDialPos > 99) newDialPos -= 100;
return newDialPos;
};
inputs.forEach(move => {
const direction = move.slice(0,1);
const magnitude = Number.parseInt(move.slice(1));
dialPos = rotateDial(direction, magnitude, dialPos);
if (dialPos === 0) zeroCount++;
});
console.log(`Zero Count: ${zeroCount}`)

55
src/01b.js Normal file
View File

@ -0,0 +1,55 @@
import data from "./inputs/01ex.json" with { type: "json" };
const inputs = data.inputs;
/* Global Variables */
let zeroCount = 0;
/* Starting Dial Position */
let dialPos = 50;
/* Functions */
/**
*
* @param {String} direction - "L" or "R"
* @param {Number} magnitude - Number of positions to rotate the dial
* @param {Number} begDialPos - dialPos before any movement
* @returns {Number} newDialPos
*/
function rotateDial(direction, magnitude, begDialPos) {
let newDialPos = begDialPos;
switch(direction) {
case 'L':
newDialPos -= magnitude % 100;
break;
case 'R':
newDialPos += magnitude % 100;
break;
default:
throw new Error("Invalid direction provided.");
}
if (newDialPos < 0) newDialPos += 100;
if (newDialPos > 99) newDialPos -= 100;
return newDialPos;
};
function countRotations(magnitude) {
}
inputs.forEach(move => {
const direction = move.slice(0,1);
const magnitude = Number.parseInt(move.slice(1));
dialPos = rotateDial(direction, magnitude, dialPos);
if (dialPos === 0) zeroCount++;
});
console.log(`Zero Count: ${zeroCount}`)
console.log(zeroCount);
/*
* lower than 6813
* higher than 5795
* not 6324
* not 6221
*/

57
src/02a.js Normal file
View File

@ -0,0 +1,57 @@
import data from "./inputs/02ex.json" with { type: "json" };
/**
*
* @param {String} rawRanges Raw input from AoC
* @returns {Array<Object>} - Array of ranges to check
*/
function parseRanges(rawRanges) {
// Set up the Array to store the range objects
/** @type {Array<Object>} */
const parsedRanges = new Array();
// Separate the raw string into subsets of ##-##
/** @type {Array<String>} */
const tmpRanges = rawRanges.split(",");
tmpRanges.forEach(range => {
// Separate each string to get the lower and upper bounds
/** @type {Array<String>} */
const tmp = range.split("-");
// Coerce the strings into ints
/** @type {Number} */
const lower = tmp[0];
/** @type {Number} */
const upper = tmp[1];
// Add the range
parsedRanges.push({
lowerBound: lower,
upperBound: upper
});
});
return parsedRanges;
}
/**
*
* @param {Number} id
*/
function checkValidity(id) {
const re = new RegExp("[0-9][0-9]{2,}");
const idStr = id.toString();
const matches = idStr.match(re);
if (typeof matches[0] === 'string') return true;
return false;
}
const ranges = parseRanges(data.inputs);
const invalidIds = new Array();
ranges.forEach(range => {
for(let i = range.lowerBound; i <= range.upperBound; i++) {
if (!(checkValidity(i))) {
invalidIds.push(i);
}
};
});

56
src/03.js Normal file
View File

@ -0,0 +1,56 @@
const { inputs } = require("./inputs/03.json");
const joltageBanks = new Array();
/**
*
* @param {String} bank
*/
function getHighestJoltages(bank) {
// Turn the string into an array of battery joltage values and their associated index
const joltages = new Array();
for (let i = 0; i < bank.length; i++) {
joltages.push({
"value": bank[i],
"index": i
});
}
// Sort them highest to lowest
joltages.sort((a, b) => {
if (a.value < b.value) return 1;
else return -1;
});
const finalJoltages = [
joltages[0]
];
let foundValid = false;
let j = 1;
while (foundValid == false) {
if (joltages[j].index > joltages[0].index) {
finalJoltages.push(joltages[j]);
foundValid = true;
} else {
j++;
}
}
return finalJoltages;
}
let counter = 0;
inputs.forEach(bank => {
console.log("Bank: ", counter);
const highestJoltages = getHighestJoltages(bank);
counter++;
});
let joltageSum = 0;
joltageBanks.forEach(bank => {
const value = Number.parseInt(bank);
joltageSum += value;
});
console.log(joltageSum);