-
-
Save Yazir/82e4cbaba829133007ac3314431f7476 to your computer and use it in GitHub Desktop.
aoc 2023 - #3 part 1+2
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
{ | |
const input = document.body.innerText | |
function parseMap() { | |
const rowsUnparsed = input.split("\n") | |
const rows = [] | |
for (let y = 0; y < rowsUnparsed.length; y++) { | |
rows[y] = rowsUnparsed[y].split("") | |
} | |
// Group numbers | |
/** @typedef {{number: string, x: number, y: number, isAdjacent: bool}} Group */ | |
/** @type {Group[]} */ | |
const groups = [] | |
/** @type {{x:number, y:number}[]} */ | |
const gears = [] | |
let numberBuffer = "" | |
for (let y = 0; y < rows.length; y++) { | |
for (let x = 0; x < rows[y].length; x++) { | |
const current = rows[y][x] | |
const next = rows[y]?.[x+1] ?? "." | |
const isNumber = new RegExp(/\d/) | |
if (isNumber.test(current)) { | |
numberBuffer += current | |
} | |
if (numberBuffer != "" && !isNumber.test(next)) { | |
groups.push({ | |
number: numberBuffer, | |
x: x - (numberBuffer.length-1), | |
y: y | |
}) | |
numberBuffer = "" | |
} | |
if (current == "*") { | |
gears.push({ | |
x: x, | |
y: y | |
}) | |
} | |
} | |
} | |
// Check adjacents | |
for (const group of groups) { | |
for (let x = 0; x < group.number.length+2; x++) { | |
if (group.isAdjacent) { | |
break | |
} | |
for (let y = 0; y < 3; y++) { | |
const bx = group.x + x | |
const by = group.y + y - 1 | |
if (bx < 0 || by < 0 || bx > rows[0].length - 1 || by > rows.length - 1) { | |
continue | |
} | |
const current = rows[by][bx] | |
const isNotSymbol = new RegExp(/\d|\./, "g") | |
if (isNotSymbol.test(current)) { | |
continue | |
} | |
group.isAdjacent = true | |
} | |
} | |
} | |
// Sum up adjacents | |
const sum = groups | |
.filter(g => g.isAdjacent) | |
.reduce((prev, now) => prev + Number(now.number), 0) | |
console.log("Answer p1:", sum) | |
// Find * adjacents | |
/** @type {Group[][]} */ | |
const gearAdjacents = [] | |
for (let i = 0; i < gears.length; i++) { | |
const {x, y} = gears[i]; | |
/** @type { Set<Group> } */ | |
const adjacentSet = new Set() | |
for (by = y - 1; by < y + 2; by++) { | |
for (bx = x - 1; bx < x + 2; bx ++) { | |
if (by < 0 || by > rows.length-1 || bx < 0 || bx > rows[by].length-1) { | |
continue | |
} | |
groups | |
.filter(g => by == g.y && bx >= g.x && bx < g.x + g.number.length) | |
.forEach(g => adjacentSet.add(g)) | |
} | |
} | |
gearAdjacents.push(Array.from(adjacentSet.values())) | |
} | |
console.log(gearAdjacents) | |
const gearMulti = gearAdjacents | |
.filter(gs => gs.length == 2) | |
.reduce((prev, current) => prev + current | |
.reduce((prev, curr) => prev * Number(curr.number), 1), 0) | |
console.log("Answer p2:", gearMulti) | |
} | |
parseMap() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment