Restructure folders and add day 3's solution
This commit is contained in:
parent
ecab4f263a
commit
519c1b63cc
6 changed files with 123 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import { readFile } from "fs/promises";
|
import { readFile } from "fs/promises";
|
||||||
import { SortedList } from "../../utils/javascript/SortedList.mjs";
|
import { SortedList } from "../utils/javascript/SortedList.mjs";
|
||||||
|
|
||||||
|
|
||||||
const fileToRead = `actual.input`;
|
const fileToRead = `actual.input`;
|
||||||
36
javascript/day_02/part1.mjs
Normal file
36
javascript/day_02/part1.mjs
Normal file
|
|
@ -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)
|
||||||
51
javascript/day_02/part2.mjs
Normal file
51
javascript/day_02/part2.mjs
Normal file
|
|
@ -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);
|
||||||
35
javascript/day_03/solution.mjs
Normal file
35
javascript/day_03/solution.mjs
Normal file
|
|
@ -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 = /(?<cmd>mul|do|don't)\(((?<a>\d{1,3}),(?<b>\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}`);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue