generated from eric/adventofcode2023
Compare commits
12 Commits
90378ddd6f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42b2af4452 | ||
|
|
462508c90f | ||
|
|
83911a58ec | ||
|
|
ea72130ebc | ||
|
|
2cd532c419 | ||
|
|
3f9cfd121a | ||
|
|
f17d99bec1 | ||
|
|
06de865c12 | ||
|
|
26d828fe1a | ||
|
|
a466e30de5 | ||
|
|
8223e82097 | ||
|
|
3e11588b2e |
51
01/code.js
51
01/code.js
@@ -1,27 +1,60 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
const inputArray = readFileSync('sample.txt').toString().split("\n");
|
||||
// const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
// const inputArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
// Part One
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
let zeroCount = 0;
|
||||
let currentPosition = 50;
|
||||
|
||||
for (const element of inputArray) {
|
||||
const instruction = element;
|
||||
const direction = instruction[0];
|
||||
const steps = Number.parseInt(instruction.slice(1));
|
||||
|
||||
if (direction === 'L') {
|
||||
currentPosition -= steps;
|
||||
} else {
|
||||
currentPosition += steps;
|
||||
}
|
||||
|
||||
if (currentPosition === 0 || (currentPosition % 100) === 0) {
|
||||
zeroCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
console.log(zeroCount);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
let zeroCount2 = 0;
|
||||
let currentPosition2 = 50;
|
||||
|
||||
for (const element of inputArray) {
|
||||
const instruction = element;
|
||||
const direction = instruction[0];
|
||||
const steps = Number.parseInt(instruction.slice(1));
|
||||
|
||||
if (direction === 'L') {
|
||||
for (let i = 1; i < steps + 1; i++) {
|
||||
currentPosition2 -= 1;
|
||||
if (currentPosition2 === 0 || (currentPosition2 % 100) === 0) {
|
||||
zeroCount2++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (let i = 1; i < steps + 1; i++) {
|
||||
currentPosition2 += 1;
|
||||
if (currentPosition2 === 0 || (currentPosition2 % 100) === 0) {
|
||||
zeroCount2++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
console.log();
|
||||
console.log(zeroCount2);
|
||||
|
||||
// functions
|
||||
|
||||
|
||||
58
02/code.js
58
02/code.js
@@ -1,27 +1,65 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
const inputArray = readFileSync('sample.txt').toString().split("\n");
|
||||
// const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
// const inputArray = readFileSync('sample.txt').toString().split(",");
|
||||
const inputArray = readFileSync('input.txt').toString().split(",");
|
||||
|
||||
// Part One
|
||||
console.time("part1");
|
||||
let sum = 0;
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
for (const element of inputArray) {
|
||||
const contents = element.split("-");
|
||||
const start = Number.parseInt(contents[0]);
|
||||
const end = Number.parseInt(contents[1]);
|
||||
|
||||
for (let sku = start; sku <= end; sku++) {
|
||||
const skuString = sku.toString();
|
||||
const digits = skuString.length
|
||||
if (digits % 2 === 0) {
|
||||
if (skuString.slice(0, digits / 2) === skuString.slice(digits / 2)) {
|
||||
sum += sku;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
console.timeEnd("part1");
|
||||
console.log(sum);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
console.time("part2");
|
||||
let sum2 = 0;
|
||||
const repeats = [];
|
||||
|
||||
for (const element of inputArray) {
|
||||
const contents = element.split("-");
|
||||
const start = Number.parseInt(contents[0]);
|
||||
const end = Number.parseInt(contents[1]);
|
||||
|
||||
for (let sku = start; sku <= end; sku++) {
|
||||
const skuString = sku.toString();
|
||||
const digits = skuString.length
|
||||
const maxRepeatLength = Math.floor(digits / 2);
|
||||
for (let repeatLength = 1; repeatLength <= maxRepeatLength; repeatLength++) {
|
||||
if (digits % repeatLength === 0) {
|
||||
const maxRepeats = digits / repeatLength;
|
||||
const repeatPattern = skuString.slice(0, repeatLength);
|
||||
const repeatedPattern = repeatPattern.repeat(maxRepeats);
|
||||
if (repeatedPattern === skuString) {
|
||||
repeats.push(sku);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const uniqueRepeats = [...new Set(repeats)];
|
||||
|
||||
console.log();
|
||||
for (const repeat of uniqueRepeats) {
|
||||
sum2 += Number.parseInt(repeat);
|
||||
}
|
||||
console.timeEnd("part2");
|
||||
console.log(sum2);
|
||||
|
||||
// functions
|
||||
|
||||
|
||||
166
03/code.js
166
03/code.js
@@ -1,27 +1,169 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
const inputArray = readFileSync('sample.txt').toString().split("\n");
|
||||
// const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
// const inputArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
// Part One
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
console.time("part1");
|
||||
let sum = 0;
|
||||
|
||||
for (const element of inputArray) {
|
||||
const length = element.length;
|
||||
let firstDigitLocation = 0;
|
||||
let firstDigit = 1;
|
||||
let lastDigit = 0;
|
||||
|
||||
for (let i = 0; i < length - 1; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > firstDigit) {
|
||||
firstDigit = digit;
|
||||
firstDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = firstDigitLocation + 1; i < length; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > lastDigit) {
|
||||
lastDigit = digit;
|
||||
}
|
||||
}
|
||||
const joltage = (firstDigit * 10) + lastDigit;
|
||||
sum += joltage;
|
||||
}
|
||||
|
||||
console.log();
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(sum);
|
||||
|
||||
// Part Two
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
console.time("part2");
|
||||
let sum2 = 0;
|
||||
|
||||
for (const element of inputArray) {
|
||||
const length = element.length;
|
||||
let firstDigitLocation = 0;
|
||||
let firstDigit = 1;
|
||||
let secondDigitLocation = 1;
|
||||
let secondDigit = 0;
|
||||
let thirdDigitLocation = 2;
|
||||
let thirdDigit = 0;
|
||||
let fourthDigitLocation = 3;
|
||||
let fourthDigit = 0;
|
||||
let fifthDigitLocation = 4;
|
||||
let fifthDigit = 0;
|
||||
let sixthDigitLocation = 5;
|
||||
let sixthDigit = 0;
|
||||
let seventhDigitLocation = 6;
|
||||
let seventhDigit = 0;
|
||||
let eighthDigitLocation = 7;
|
||||
let eighthDigit = 0;
|
||||
let ninthDigitLocation = 8;
|
||||
let ninthDigit = 0;
|
||||
let tenthDigitLocation = 9;
|
||||
let tenthDigit = 0;
|
||||
let eleventhDigitLocation = 10;
|
||||
let eleventhDigit = 0;
|
||||
let lastDigit = 0;
|
||||
|
||||
for (let i = 0; i < length - 11; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > firstDigit) {
|
||||
firstDigit = digit;
|
||||
firstDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = firstDigitLocation + 1; i < length - 10; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > secondDigit) {
|
||||
secondDigit = digit;
|
||||
secondDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = secondDigitLocation + 1; i < length - 9 ; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > thirdDigit) {
|
||||
thirdDigit = digit;
|
||||
thirdDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = thirdDigitLocation + 1; i < length - 8; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > fourthDigit) {
|
||||
fourthDigit = digit;
|
||||
fourthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = fourthDigitLocation + 1; i < length - 7; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > fifthDigit) {
|
||||
fifthDigit = digit;
|
||||
fifthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = fifthDigitLocation + 1; i < length - 6; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > sixthDigit) {
|
||||
sixthDigit = digit;
|
||||
sixthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = sixthDigitLocation + 1; i < length - 5; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > seventhDigit) {
|
||||
seventhDigit = digit;
|
||||
seventhDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = seventhDigitLocation + 1; i < length - 4; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > eighthDigit) {
|
||||
eighthDigit = digit;
|
||||
eighthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = eighthDigitLocation + 1; i < length - 3; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > ninthDigit) {
|
||||
ninthDigit = digit;
|
||||
ninthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = ninthDigitLocation + 1; i < length - 2; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > tenthDigit) {
|
||||
tenthDigit = digit;
|
||||
tenthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = tenthDigitLocation + 1; i < length - 1; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > eleventhDigit) {
|
||||
eleventhDigit = digit;
|
||||
eleventhDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = eleventhDigitLocation + 1; i < length; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > lastDigit) {
|
||||
lastDigit = digit;
|
||||
}
|
||||
}
|
||||
|
||||
const joltage = (firstDigit * 100000000000) +
|
||||
(secondDigit * 10000000000) +
|
||||
(thirdDigit * 1000000000) +
|
||||
(fourthDigit * 100000000) +
|
||||
(fifthDigit * 10000000) +
|
||||
(sixthDigit * 1000000) +
|
||||
(seventhDigit * 100000) +
|
||||
(eighthDigit * 10000) +
|
||||
(ninthDigit * 1000) +
|
||||
(tenthDigit * 100) +
|
||||
(eleventhDigit * 10) +
|
||||
lastDigit;
|
||||
sum2 += joltage;
|
||||
}
|
||||
|
||||
|
||||
console.log();
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(sum2);
|
||||
// functions
|
||||
|
||||
|
||||
143
04/code.js
Normal file
143
04/code.js
Normal file
@@ -0,0 +1,143 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = [].concat(sampleArray);
|
||||
} else {
|
||||
usedArray = [].concat(inputArray);
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
let sum = 0;
|
||||
|
||||
for (let i = 0; i < usedArray.length; i++) {
|
||||
let row = usedArray[i];
|
||||
for (let j = 0; j < row.length; j++) {
|
||||
let cell = row[j];
|
||||
if (cell === '@') {
|
||||
let openSides = 0;
|
||||
const left = j > 0 ? row[j - 1] : '.';
|
||||
const right = j < row.length - 1 ? row[j + 1] : '.';
|
||||
const up = i > 0 ? usedArray[i - 1][j] : '.';
|
||||
const down = i < usedArray.length - 1 ? usedArray[i + 1][j] : '.';
|
||||
const upperLeft = i > 0 && j > 0 ? usedArray[i - 1][j - 1] : '.';
|
||||
const upperRight = i > 0 && j < row.length - 1 ? usedArray[i - 1][j + 1] : '.';
|
||||
const lowerLeft = i < usedArray.length - 1 && j > 0 ? usedArray[i + 1][j - 1] : '.';
|
||||
const lowerRight = i < usedArray.length - 1 && j < row.length - 1 ? usedArray[i + 1][j + 1] : '.';
|
||||
if (left === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (right === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (up === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (down === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (upperLeft === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (upperRight === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (lowerLeft === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (lowerRight === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (openSides >= 5) {
|
||||
sum++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(sum);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
let usedArray2 = [];
|
||||
if (sampleMode) {
|
||||
usedArray2 = [].concat(sampleArray);
|
||||
} else {
|
||||
usedArray2 = [].concat(inputArray);
|
||||
}
|
||||
|
||||
console.time("part2");
|
||||
let sum2 = 0;
|
||||
let done = false;
|
||||
|
||||
while (!done) {
|
||||
getOpenSides(sum2, usedArray2);
|
||||
}
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(sum2);
|
||||
|
||||
// functions
|
||||
|
||||
function getOpenSides(startingSum, usedArray) {
|
||||
const newSum = JSON.parse(JSON.stringify(startingSum));
|
||||
for (let i = 0; i < usedArray.length; i++) {
|
||||
let row = [...usedArray[i].split('')];
|
||||
for (let j = 0; j < row.length; j++) {
|
||||
let cell = row[j];
|
||||
if (cell === '@') {
|
||||
let openSides = 0;
|
||||
const left = j > 0 ? row[j - 1] : '.';
|
||||
const right = j < row.length - 1 ? row[j + 1] : '.';
|
||||
const up = i > 0 ? usedArray[i - 1][j] : '.';
|
||||
const down = i < usedArray.length - 1 ? usedArray[i + 1][j] : '.';
|
||||
const upperLeft = i > 0 && j > 0 ? usedArray[i - 1][j - 1] : '.';
|
||||
const upperRight = i > 0 && j < row.length - 1 ? usedArray[i - 1][j + 1] : '.';
|
||||
const lowerLeft = i < usedArray.length - 1 && j > 0 ? usedArray[i + 1][j - 1] : '.';
|
||||
const lowerRight = i < usedArray.length - 1 && j < row.length - 1 ? usedArray[i + 1][j + 1] : '.';
|
||||
if (left === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (right === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (up === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (down === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (upperLeft === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (upperRight === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (lowerLeft === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (lowerRight === '.') {
|
||||
openSides++;
|
||||
}
|
||||
if (openSides >= 5) {
|
||||
sum2++;
|
||||
row[j] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
usedArray[i] = row.join('');
|
||||
}
|
||||
|
||||
if (sum2 === newSum) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
97
05/code.js
Normal file
97
05/code.js
Normal file
@@ -0,0 +1,97 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
|
||||
const ranges = [];
|
||||
const ingredients = [];
|
||||
const validIngredients = [];
|
||||
|
||||
for (const element of usedArray) {
|
||||
if (element.includes("-")) {
|
||||
const contents = element.split("-");
|
||||
const start = Number.parseInt(contents[0]);
|
||||
const end = Number.parseInt(contents[1]);
|
||||
ranges.push({ start, end });
|
||||
} else if (element !== "") {
|
||||
ingredients.push(Number.parseInt(element));
|
||||
}
|
||||
}
|
||||
|
||||
for (const ingredient of ingredients) {
|
||||
for (const range of ranges) {
|
||||
if (ingredient >= range.start && ingredient <= range.end) {
|
||||
validIngredients.push(ingredient);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(validIngredients.length);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
|
||||
const ranges2 = [];
|
||||
|
||||
for (const element of usedArray) {
|
||||
if (element.includes("-")) {
|
||||
const contents = element.split("-");
|
||||
const start = Number.parseInt(contents[0]);
|
||||
const end = Number.parseInt(contents[1]);
|
||||
ranges2.push({ start, end });
|
||||
}
|
||||
}
|
||||
|
||||
ranges2.sort((a, b) => a.start - b.start);
|
||||
let processing = true;
|
||||
|
||||
while (processing) {
|
||||
compactRanges();
|
||||
}
|
||||
|
||||
// Five more times for fun, just in case
|
||||
for (let i = 0; i < 5; i++) {
|
||||
compactRanges();
|
||||
}
|
||||
|
||||
let sum = 0;
|
||||
for (const range of ranges2) {
|
||||
sum += range.end - range.start + 1;
|
||||
}
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(sum);
|
||||
|
||||
// functions
|
||||
|
||||
function compactRanges() {
|
||||
let continuing = false;
|
||||
let i = 0;
|
||||
while (i < ranges2.length - 1) {
|
||||
if (ranges2[i + 1].start <= ranges2[i].end + 1) {
|
||||
ranges2[i].end = Math.max(ranges2[i].end, ranges2[i + 1].end);
|
||||
ranges2.splice(i + 1, 1);
|
||||
continuing = true;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
processing = continuing;
|
||||
}
|
||||
135
06/code.js
Normal file
135
06/code.js
Normal file
@@ -0,0 +1,135 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
|
||||
let total = 0;
|
||||
|
||||
const problems = [];
|
||||
for (const row of usedArray) {
|
||||
const contents = row.trim().split(/\s+/);
|
||||
|
||||
for (let i = 0; i < contents.length; i++) {
|
||||
const element = contents[i];
|
||||
if (!problems[i]) {
|
||||
problems[i] = [];
|
||||
}
|
||||
problems[i].push(element);
|
||||
}
|
||||
}
|
||||
|
||||
for (let problem of problems) {
|
||||
problem = problem.reverse();
|
||||
const operator = problem.shift();
|
||||
let answer = operator === '+' ? 0 : 1;
|
||||
for (const number of problem) {
|
||||
if (operator === '+') {
|
||||
answer += Number.parseInt(number);
|
||||
} else {
|
||||
answer *= Number.parseInt(number);
|
||||
}
|
||||
}
|
||||
total += answer;
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(total);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
|
||||
let total2 = 0;
|
||||
|
||||
let newInput = JSON.parse(JSON.stringify(usedArray));
|
||||
for (let rowIndex = 0; rowIndex < newInput.length; rowIndex++) {
|
||||
const row = newInput[rowIndex];
|
||||
newInput[rowIndex] = row.split("");
|
||||
}
|
||||
|
||||
const problemStarts = [];
|
||||
const operatorRowArray = newInput[usedArray.length - 1];
|
||||
for (let i = 0; i < operatorRowArray.length; i++) {
|
||||
if (["*", "+"].includes(operatorRowArray[i])) {
|
||||
problemStarts.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
for (let rowIndex = 0; rowIndex < usedArray.length; rowIndex++) {
|
||||
const row = usedArray[rowIndex];
|
||||
for (let i = 0; i < row.length; i++) {
|
||||
const char = row[i];
|
||||
if (char === " " && !problemStarts.includes(i + 1)) {
|
||||
newInput[rowIndex][i] = '#';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let rowIndex = 0; rowIndex < newInput.length; rowIndex++) {
|
||||
const row = newInput[rowIndex];
|
||||
newInput[rowIndex] = row.join("");
|
||||
}
|
||||
|
||||
const problems2 = [];
|
||||
for (const row of newInput) {
|
||||
const contents = row.trim().split(/\s+/);
|
||||
for (let i = 0; i < contents.length; i++) {
|
||||
const element = contents[i];
|
||||
if (!problems2[i]) {
|
||||
problems2[i] = [];
|
||||
}
|
||||
problems2[i].push(element);
|
||||
}
|
||||
}
|
||||
|
||||
let problemsVertical = [];
|
||||
for (let i = 0; i < problems2.length; i++) {
|
||||
const problem = problems2[i];
|
||||
let vertProblem = [];
|
||||
for (let j = 0; j < problem.length - 1; j++) {
|
||||
const newElement =
|
||||
(problem[0] && problem[0][j] && problem[0][j] !== '#' ? problem[0][j] : '') +
|
||||
(problem[1] && problem[1][j] && problem[1][j] !== '#' ? problem[1][j] : '') +
|
||||
(problem[2] && problem[2][j] && problem[2][j] !== '#' ? problem[2][j] : '') +
|
||||
(problem[3] && problem[3][j] && problem[3][j] !== '#' ? problem[3][j] : '') +
|
||||
(problem[4] && problem[4][j] && problem[4][j] !== '#' ? problem[4][j] : '');
|
||||
if (Number.parseInt(newElement)) {
|
||||
vertProblem.push(Number.parseInt(newElement));
|
||||
}
|
||||
}
|
||||
vertProblem.push(problem[problem.length - 1]);
|
||||
problemsVertical.push(vertProblem);
|
||||
}
|
||||
|
||||
for (let problem of problemsVertical) {
|
||||
problem = problem.reverse();
|
||||
const operator = problem.shift();
|
||||
let answer = operator.includes('+') ? 0 : 1;
|
||||
for (const number of problem) {
|
||||
if (operator.includes('+')) {
|
||||
answer += Number.parseInt(number);
|
||||
} else {
|
||||
answer *= Number.parseInt(number);
|
||||
}
|
||||
}
|
||||
total2 += answer;
|
||||
}
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(total2);
|
||||
|
||||
// functions
|
||||
103
07/code.js
Normal file
103
07/code.js
Normal file
@@ -0,0 +1,103 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
|
||||
let newInput = JSON.parse(JSON.stringify(usedArray));
|
||||
for (let i = 0; i < newInput.length; i++) {
|
||||
newInput[i] = newInput[i].split("");
|
||||
}
|
||||
|
||||
for (let i = 1; i < newInput.length; i++) {
|
||||
for (let j = 0; j < newInput[i].length; j++) {
|
||||
if ((newInput[i - 1][j] === '|' || newInput[i - 1][j] === 'S') && newInput[i][j] === '.') {
|
||||
newInput[i][j] = '|';
|
||||
}
|
||||
if (newInput[i - 1][j] === '|' && newInput[i][j] === '^') {
|
||||
if (j > 0 && newInput[i][j - 1] === '.') {
|
||||
newInput[i][j - 1] = '|';
|
||||
}
|
||||
}
|
||||
if (newInput[i - 1][j] === '|' && newInput[i][j] === '^') {
|
||||
if (j < newInput[i].length - 1 && newInput[i][j + 1] === '.') {
|
||||
newInput[i][j + 1] = '|';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let splits = 0;
|
||||
|
||||
for (let i = 1; i < newInput.length; i++) {
|
||||
for (let j = 0; j < newInput[i].length; j++) {
|
||||
if (newInput[i][j] === '^' && newInput[i - 1][j] === '|') {
|
||||
splits++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(splits);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
|
||||
newInput = JSON.parse(JSON.stringify(usedArray));
|
||||
for (let i = 0; i < newInput.length; i++) {
|
||||
newInput[i] = newInput[i].split("");
|
||||
}
|
||||
|
||||
for (let i = 1; i < newInput.length; i++) {
|
||||
for (let j = 0; j < newInput[i].length; j++) {
|
||||
if (newInput[i - 1][j] === 'S') {
|
||||
newInput[i][j] = 1;
|
||||
}
|
||||
if (Number.parseInt(newInput[i - 1][j]) > 0 && Number.parseInt(newInput[i][j]) > 0) {
|
||||
newInput[i][j] = newInput[i][j] + newInput[i - 1][j];
|
||||
}
|
||||
if ((Number.parseInt(newInput[i - 1][j]) > 0) && newInput[i][j] === '.') {
|
||||
newInput[i][j] = newInput[i - 1][j];
|
||||
}
|
||||
if (Number.parseInt(newInput[i - 1][j]) > 0 && newInput[i][j] === '^') {
|
||||
if (j > 0 && newInput[i][j - 1] === '.') {
|
||||
newInput[i][j - 1] = newInput[i - 1][j];
|
||||
} else if (j > 0 && Number.parseInt(newInput[i][j - 1]) > 0) {
|
||||
newInput[i][j - 1] = Number.parseInt(newInput[i][j - 1]) + newInput[i - 1][j];
|
||||
}
|
||||
if (j < newInput[i].length - 1 && newInput[i][j + 1] === '.') {
|
||||
newInput[i][j + 1] = newInput[i - 1][j];
|
||||
} else if (j < newInput[i].length - 1 && Number.parseInt(newInput[i][j + 1]) > 0) {
|
||||
newInput[i][j + 1] = Number.parseInt(newInput[i][j + 1]) + newInput[i - 1][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let timelines = 0;
|
||||
|
||||
const bottomRow = newInput[newInput.length - 1];
|
||||
for (let i = 0; i < bottomRow.length; i++) {
|
||||
if (Number.parseInt(bottomRow[i]) > 0) {
|
||||
timelines += Number.parseInt(bottomRow[i]);
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(timelines);
|
||||
|
||||
// functions
|
||||
@@ -1,27 +1,38 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
const inputArray = readFileSync('sample.txt').toString().split("\n");
|
||||
// const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
let sampleMode = true;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
console.time("part1");
|
||||
for (const element of usedArray) {
|
||||
const contents = element.split(" ");
|
||||
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log();
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
console.time("part2");
|
||||
for (const element of usedArray) {
|
||||
const contents = element.split(" ");
|
||||
|
||||
}
|
||||
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log();
|
||||
|
||||
// functions
|
||||
|
||||
|
||||
3
package.json
Normal file
3
package.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
Reference in New Issue
Block a user