Created
December 5, 2023 10:29
-
-
Save a-rbsn/3c3d6239335d5425c320309b92aec2ab to your computer and use it in GitHub Desktop.
[2023 Day 3 (Part 1)] [JS] I'm missing something but my head hurts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs"); | |
try { | |
var data = fs.readFileSync("day3input.txt", "utf8"); | |
} catch (e) { | |
console.log("Error:", e.stack); | |
} | |
const separatedLines = data.split(/\r?\n/); | |
separatedLines.pop(); | |
const reg = /[^.\d]/; | |
const parts = []; | |
const symbols = []; | |
let ans = 0; | |
separatedLines.map((line, i) => { | |
line.split("").map((char, j) => { | |
let isSymbol = reg.test(char); | |
if (isSymbol) { | |
symbols.push(char); | |
checkForNums({ x: j, y: i }); | |
} | |
}); | |
}); | |
function checkForNums(symbol) { | |
for (let i = -1; i <= 1; i++) { | |
for (let j = -1; j <= 1; j++) { | |
let x = symbol.x + j; | |
let y = symbol.y + i; | |
let cell = separatedLines[y] ? separatedLines[y][x] : null ? separatedLines[y][x] : null; | |
if (isNum(cell)) { | |
let prev = isNum(separatedLines[y][x - 1]); | |
let next = isNum(separatedLines[y][x + 1]); | |
// ....... | |
// .145... | |
// ...*... | |
// ...123. | |
// | |
if (j === -1 && !next) { | |
parts.push(getNumLeft({ x, y })); | |
} | |
if (j === 0) { | |
if (prev && !next) { | |
parts.push(getNumLeft({ x, y })); | |
} | |
if (prev && next) { | |
parts.push(getNumMiddle({ x, y })); | |
} | |
if (!prev && next) { | |
parts.push(getNumRight({ x, y })); | |
} | |
} | |
if (j === 1 && !prev) { | |
parts.push(getNumRight({ x, y })); | |
} | |
// if solo number | |
/* if (!prev && !next) { | |
parts.push(cell); | |
} */ | |
} | |
} | |
} | |
} | |
function isNum(char) { | |
return parseInt(char) >= 0; | |
} | |
function getNumMiddle(cell) { | |
let y = cell.y; | |
let xL = cell.x; | |
let xR = cell.x + 1; | |
let num = []; | |
while (isNum(separatedLines[y][xL])) { | |
num.unshift(separatedLines[y][xL]); | |
xL -= 1; | |
} | |
while (isNum(separatedLines[y][xR])) { | |
num.push(separatedLines[y][xR]); | |
xR += 1; | |
} | |
return num.join(""); | |
} | |
function getNumLeft(cell) { | |
let y = cell.y; | |
let x = cell.x; | |
let num = []; | |
while (isNum(separatedLines[y][x])) { | |
num.unshift(separatedLines[y][x]); | |
x -= 1; | |
} | |
return num.join(""); | |
} | |
function getNumRight(cell) { | |
let y = cell.y; | |
let x = cell.x; | |
let num = []; | |
while (isNum(separatedLines[y][x])) { | |
num.push(separatedLines[y][x]); | |
x += 1; | |
} | |
return num.join(""); | |
} | |
parts.map((num) => { | |
fs.appendFile("day3output.txt", num + "\n", (err) => { | |
if (err) console.log(err); | |
}); | |
ans += parseInt(num); | |
}); | |
console.log(ans); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment