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
31
javascript/day_01/part1.mjs
Normal file
31
javascript/day_01/part1.mjs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { readFile } from "fs/promises";
|
||||
import { SortedList } from "../utils/javascript/SortedList.mjs";
|
||||
|
||||
|
||||
const fileToRead = `actual.input`;
|
||||
|
||||
const list1 = new SortedList();
|
||||
const list2 = new SortedList();
|
||||
|
||||
const file = await readFile(fileToRead, `utf-8`);
|
||||
for (const line of file.split(`\n`)) {
|
||||
// console.log(line);
|
||||
const [ l1Add, l2Add ] = line.split(/\s+/, 2);
|
||||
list1.add(l1Add);
|
||||
list2.add(l2Add);
|
||||
};
|
||||
|
||||
if (list1.size !== list2.size) {
|
||||
console.log(`The two lists are different lengths`);
|
||||
process.exit(1);
|
||||
};
|
||||
|
||||
const size = list1.size;
|
||||
let distance = 0;
|
||||
for (var i = 0; i < size; i++) {
|
||||
const place1 = list1.at(i);
|
||||
const place2 = list2.at(i);
|
||||
distance += Math.abs(place2 - place1);
|
||||
}
|
||||
|
||||
console.log(`Total Distance: ${distance}`);
|
||||
26
javascript/day_01/part2.mjs
Normal file
26
javascript/day_01/part2.mjs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { readFile } from "fs/promises";
|
||||
|
||||
|
||||
const fileToRead = `actual.input`;
|
||||
|
||||
const list1 = {};
|
||||
const list2 = {};
|
||||
|
||||
const file = await readFile(fileToRead, `utf-8`);
|
||||
|
||||
for (const line of file.split(`\n`)) {
|
||||
const [ l1Add, l2Add ] = line.split(/\s+/, 2);
|
||||
|
||||
list1[l1Add] ??= 0;
|
||||
list1[l1Add] += 1;
|
||||
|
||||
list2[l2Add] ??= 0;
|
||||
list2[l2Add] += 1;
|
||||
};
|
||||
|
||||
let similarity = 0;
|
||||
for (const location in list1) {
|
||||
similarity += list1[location] * (location * (list2[location] ?? 0))
|
||||
};
|
||||
|
||||
console.log(`Similarity: ${similarity}`);
|
||||
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}`);
|
||||
65
javascript/utils/javascript/SortedList.mjs
Normal file
65
javascript/utils/javascript/SortedList.mjs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
export class SortedList {
|
||||
|
||||
#data = [];
|
||||
|
||||
add(value) {
|
||||
|
||||
// Simple cases that we can short-circuit on
|
||||
if (this.#data.length === 0 || this.#data.at(-1) < value) {
|
||||
this.#data.push(value);
|
||||
return;
|
||||
};
|
||||
if (this.#data.at(0) > value) {
|
||||
this.#data.unshift(value);
|
||||
return
|
||||
};
|
||||
|
||||
let locationToAdd;
|
||||
let lStart = 0;
|
||||
let lEnd = this.#data.length;
|
||||
do {
|
||||
let midpoint = Math.floor((lStart + lEnd) / 2);
|
||||
|
||||
if (lStart > lEnd) {
|
||||
// console.log(`breaking for ${value} at lStart=${lStart}, lEnd=${lEnd}`)
|
||||
locationToAdd = lStart;
|
||||
break
|
||||
};
|
||||
|
||||
let valAtMidpoint =this.#data.at(midpoint);
|
||||
if (valAtMidpoint < value) {
|
||||
// console.log(`${valAtMidpoint} < ${value}, using second half of list`)
|
||||
lStart = midpoint + 1;
|
||||
}
|
||||
else if (valAtMidpoint > value) {
|
||||
// console.log(`${valAtMidpoint} > ${value}, using first half of list`)
|
||||
lEnd = midpoint - 1;
|
||||
}
|
||||
else {
|
||||
locationToAdd = midpoint;
|
||||
}
|
||||
} while (locationToAdd === undefined);
|
||||
|
||||
// console.log(`locationToAdd (${value}) = ${locationToAdd}`);
|
||||
this.#data.splice(locationToAdd, 0, value);
|
||||
};
|
||||
|
||||
get size() {
|
||||
return this.#data.length;
|
||||
}
|
||||
|
||||
at(index) {
|
||||
return this.#data.at(index);
|
||||
}
|
||||
|
||||
*iter() {
|
||||
// console.log(JSON.stringify(this.#data))
|
||||
const size = this.#data.length;
|
||||
for (var i = 0; i < size; i++) {
|
||||
// if (i === size - 1) {
|
||||
// yield this.#data[i]
|
||||
// }
|
||||
yield this.#data[i];
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue