day 6 complete!

This commit is contained in:
2025-12-06 02:27:12 -05:00
parent 93f795cd2c
commit 6b4f1e26a5
8 changed files with 318 additions and 0 deletions

49
day06/js/day06a.js Normal file
View File

@@ -0,0 +1,49 @@
import { promises as fsPromises } from "node:fs";
async function asyncReadFile(filename) {
try {
const contents = await fsPromises.readFile(filename, "utf-8");
const arr = contents.split(/\r?\n/);
return arr;
} catch (err) {
console.error(err);
}
}
/*
const data = `
123 328 51 64
45 64 387 23
6 98 215 314
* + * +
`.trim()
.split("\n");
*/
const data = await asyncReadFile("../input.txt");
const parsedData = data.map((val) => val.trim().split(/\s+/));
const rowCount = parsedData.length,
colCount = rowCount ? parsedData[0].length : 0;
const numRegex = /\d+/;
let total = 0;
for (let c = 0; c < colCount; c++) {
let operands = [];
let result = 0;
for (let r = 0; r < rowCount; r++) {
const val = parsedData[r][c];
if (numRegex.test(val)) operands.push(val * 1);
else if (val === "+")
result = operands.reduce((acc, curr) => acc + curr, 0);
else if (val === "*")
result = operands.reduce((acc, curr) => acc * curr, 1);
}
console.log(`> result: ${result}`);
total += result;
}
console.log(`>> total: ${total}`);

81
day06/js/day06b.js Normal file
View File

@@ -0,0 +1,81 @@
import { promises as fsPromises } from "node:fs";
async function asyncReadFile(filename) {
try {
const contents = await fsPromises.readFile(filename, "utf-8");
const arr = contents.split(/\r?\n/);
return arr;
} catch (err) {
console.error(err);
}
}
const findDividers = (rows) => {
let spaceIndexes = [];
rows.forEach((row, rowIndex) => {
if (row == "") return;
if (rowIndex === 0) {
Array.from(row).forEach((theChar, charIndex) => {
if (theChar === " ") spaceIndexes.push(charIndex);
});
return;
}
const rowArray = Array.from(row);
spaceIndexes = spaceIndexes.filter(
(spaceIndex) => rowArray[spaceIndex] === " "
);
});
return spaceIndexes;
};
const calculateValues = (operands, operation) => {
if (operation === "+")
return operands.reduce((acc, curr) => acc + curr * 1, 0);
else if (operation === "*")
return operands.reduce((acc, curr) => acc * curr, 1);
};
/*
const data = `
123 328 51 64
45 64 387 23
6 98 215 314
* + * +
`
.trim()
.split("\n");
*/
const data = await asyncReadFile("../input.txt");
let total = 0;
const dividers = findDividers(data);
let newOperands = [];
let operation = "";
for (let c = data[0].length - 1; c >= 0; c--) {
if (dividers.includes(c) || c === -1) {
const result = calculateValues(newOperands, operation);
total += result;
console.log(`> result: ${result}`);
operation = "";
newOperands = [];
}
let newOperandParts = [];
data.forEach((row) => {
const charArray = Array.from(row);
if (/\d+/.test(charArray[c])) newOperandParts.push(charArray[c]);
else if (["+", "*"].includes(charArray[c])) operation = charArray[c];
});
if (newOperandParts.join("") !== "")
newOperands.push(newOperandParts.join(""));
}
const result = calculateValues(newOperands, operation);
total += result;
console.log(`>> total: ${total}`);