From 42b2af4452ab1b491e0bc78a95fd623b334537b6 Mon Sep 17 00:00:00 2001 From: Eric Wagoner Date: Sun, 7 Dec 2025 01:38:11 -0500 Subject: [PATCH] day seven both parts --- 07/code.js | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 07/code.js diff --git a/07/code.js b/07/code.js new file mode 100644 index 0000000..464838e --- /dev/null +++ b/07/code.js @@ -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