From 519c1b63ccd634da36272d1ed54f59ddbf09b3c2 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Wed, 4 Dec 2024 21:27:20 -0700 Subject: [PATCH] Restructure folders and add day 3's solution --- .../day_01}/part1.mjs | 2 +- .../day_01}/part2.mjs | 0 javascript/day_02/part1.mjs | 36 +++++++++++++ javascript/day_02/part2.mjs | 51 +++++++++++++++++++ javascript/day_03/solution.mjs | 35 +++++++++++++ .../utils}/javascript/SortedList.mjs | 0 6 files changed, 123 insertions(+), 1 deletion(-) rename {day_01/javascript => javascript/day_01}/part1.mjs (90%) rename {day_01/javascript => javascript/day_01}/part2.mjs (100%) create mode 100644 javascript/day_02/part1.mjs create mode 100644 javascript/day_02/part2.mjs create mode 100644 javascript/day_03/solution.mjs rename {utils => javascript/utils}/javascript/SortedList.mjs (100%) diff --git a/day_01/javascript/part1.mjs b/javascript/day_01/part1.mjs similarity index 90% rename from day_01/javascript/part1.mjs rename to javascript/day_01/part1.mjs index 1994cb9..bf41cef 100644 --- a/day_01/javascript/part1.mjs +++ b/javascript/day_01/part1.mjs @@ -1,5 +1,5 @@ import { readFile } from "fs/promises"; -import { SortedList } from "../../utils/javascript/SortedList.mjs"; +import { SortedList } from "../utils/javascript/SortedList.mjs"; const fileToRead = `actual.input`; diff --git a/day_01/javascript/part2.mjs b/javascript/day_01/part2.mjs similarity index 100% rename from day_01/javascript/part2.mjs rename to javascript/day_01/part2.mjs diff --git a/javascript/day_02/part1.mjs b/javascript/day_02/part1.mjs new file mode 100644 index 0000000..8a8aadf --- /dev/null +++ b/javascript/day_02/part1.mjs @@ -0,0 +1,36 @@ +import { readFile } from "fs/promises"; + +function legalValues(a, b, gap) { + const delta = Math.abs(a - b); + console.log(`legaValues(${a}, ${b}, ${gap}){delta=${delta}} = ${delta <= gap}`); + return delta <= gap; +}; + +if (process.argv.length <= 2) { + console.error(`Needs more args >:(`). + process.exit(1); +} + +const fileToRead = process.argv[2]; +const file = await readFile(fileToRead, `utf-8`); + +let validLines = 0; +for (const line of file.split(`\n`)) { + // console.log(line) + const ints = line.split(` `) + .map(v => Number.parseInt(v)); + + const isSafe = ints + .slice(0, -1) + .map((v, i) => v - ints[i + 1]) + .every((v, _, a) => Math.sign(v) === Math.sign(a[0]) && Math.abs(v) <= 3); + + // console.log(isSafe) + + if(isSafe) { + validLines++; + }; + // break +}; + +console.log(`Number of valid reports:`, validLines) diff --git a/javascript/day_02/part2.mjs b/javascript/day_02/part2.mjs new file mode 100644 index 0000000..7f510e1 --- /dev/null +++ b/javascript/day_02/part2.mjs @@ -0,0 +1,51 @@ +import { readFile } from "fs/promises"; + +if (process.argv.length <= 3) { + console.error(`Needs more args >:(`); + process.exit(1); +} + +const fileToRead = process.argv[2]; +const gap = parseInt(process.argv[3]) +const file = await readFile(fileToRead, `utf-8`); + +let validLines = 0; +for (const line of file.split(`\n`)) { + // console.log(line) + const ints = line.split(` `) + .map(v => Number.parseInt(v)) + // .reverse() + + let isValid = true; + let errorCorrected = false; + let firstSign = null; + let firstIter = true; + for (var i = 0; i < ints.length - 1; i++) { + const a = ints[i]; + const b = ints[i + 1]; + + const sign = Math.sign( a - b ); + const delta = Math.abs( a - b ); + + firstSign ??= sign; + + const rightDirection = sign === firstSign; + const deltaWithinRange = delta <= gap; + + if (!rightDirection || !deltaWithinRange) { + isValid &&= !errorCorrected; + if (!isValid) { + break; + } + errorCorrected = true; + ints.splice(i , 1); + i--; + continue; + } + firstIter = false; + }; + console.log({ firstSign, isValid, errorCorrected, ints }) + if (isValid) validLines++; +}; + +console.log(`Number of valid reports:`, validLines); diff --git a/javascript/day_03/solution.mjs b/javascript/day_03/solution.mjs new file mode 100644 index 0000000..dc78fe5 --- /dev/null +++ b/javascript/day_03/solution.mjs @@ -0,0 +1,35 @@ +import { readFile } from "fs/promises"; + +if (process.argv.length <= 2) { + console.error(`Needs more args >:(`). + process.exit(0); +} + +const fileToRead = process.argv[2]; +const file = await readFile(fileToRead, `utf-8`); + +const pattern = /(?mul|do|don't)\(((?\d{1,3}),(?\d{1,3}))?\)/g; + +const commands = file.matchAll(pattern); + +let part1Sum = 0; +let part2Sum = 0; +let enabled = true +for (const command of commands) { + const { cmd, a, b } = command.groups; + + switch (cmd) { + case "do": enabled = true; break + case "don't": enabled = false; break + case "mul": { + part1Sum += a * b; + if (enabled) { + part2Sum += a * b; + } + break; + } + } +}; + +console.log(`Part 1 Answer: ${part1Sum}`); +console.log(`Part 2 Answer: ${part2Sum}`); diff --git a/utils/javascript/SortedList.mjs b/javascript/utils/javascript/SortedList.mjs similarity index 100% rename from utils/javascript/SortedList.mjs rename to javascript/utils/javascript/SortedList.mjs