From a0b4c83c445e1b95f5ff180059c04765024bbaef Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 4 Dec 2025 22:39:32 -0500 Subject: [PATCH] spaghetti at a wall and none of its edible --- .gitignore | 1 + src/01a.js | 42 ++++++++++++++++++++++++++++++++++++++++ src/01b.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/02a.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/03.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 211 insertions(+) create mode 100644 .gitignore create mode 100644 src/01a.js create mode 100644 src/01b.js create mode 100644 src/02a.js create mode 100644 src/03.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a785eed --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +src/inputs* \ No newline at end of file diff --git a/src/01a.js b/src/01a.js new file mode 100644 index 0000000..0fb8ac6 --- /dev/null +++ b/src/01a.js @@ -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}`) \ No newline at end of file diff --git a/src/01b.js b/src/01b.js new file mode 100644 index 0000000..06de507 --- /dev/null +++ b/src/01b.js @@ -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 + */ \ No newline at end of file diff --git a/src/02a.js b/src/02a.js new file mode 100644 index 0000000..b8aeb0d --- /dev/null +++ b/src/02a.js @@ -0,0 +1,57 @@ +import data from "./inputs/02ex.json" with { type: "json" }; + +/** + * + * @param {String} rawRanges Raw input from AoC + * @returns {Array} - Array of ranges to check + */ +function parseRanges(rawRanges) { + // Set up the Array to store the range objects + /** @type {Array} */ + const parsedRanges = new Array(); + // Separate the raw string into subsets of ##-## + /** @type {Array} */ + const tmpRanges = rawRanges.split(","); + tmpRanges.forEach(range => { + // Separate each string to get the lower and upper bounds + /** @type {Array} */ + 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); + } + }; +}); \ No newline at end of file diff --git a/src/03.js b/src/03.js new file mode 100644 index 0000000..63e3bec --- /dev/null +++ b/src/03.js @@ -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); \ No newline at end of file