day 03 part 2 finished
This commit is contained in:
@ -14,23 +14,59 @@ for (let y = 0; y < lines.length; y++) {
|
||||
structureMap.push({ name: struct[0], x: struct.index, y: y })
|
||||
})
|
||||
}
|
||||
|
||||
// --- part 1 ---------------------------------------------------------
|
||||
for (let y = 0; y < lines.length; y++) {
|
||||
const found_numbers = [...lines[y].matchAll(filter_data)]
|
||||
found_numbers.forEach(number => {
|
||||
let result = false
|
||||
console.log(`-------- ${number[0]}`)
|
||||
result = isStructAroundMe(lines, { line: lines[y], y: y, x: number.index, xEnd: number.index + number[0].length - 1 })
|
||||
if (result) res += parseInt(number[0])
|
||||
if (!result) console.log(`
|
||||
|
||||
${number[0]} has no adjacent parts!
|
||||
|
||||
`)
|
||||
res += result ? parseInt(number[0]) : 0
|
||||
})
|
||||
}
|
||||
console.log(structureMap)
|
||||
console.log(res)
|
||||
|
||||
// --- part 2 ---------
|
||||
console.log(`part2`.padEnd(30, '-'))
|
||||
let result2 = 0
|
||||
structureMap.filter(struct => struct.name === '*').forEach(structure => {
|
||||
const neighbours = findNeighbours(structure)
|
||||
result2 += neighbours.length === 2 ? neighbours.reduce((cur, next) => cur * next) : 0
|
||||
|
||||
})
|
||||
console.log(result2)
|
||||
function findNeighbours(structure) {
|
||||
const directions = getDirections(structure.x, structure.y)
|
||||
let chars = []
|
||||
directions.forEach(direction => {
|
||||
let char = lines[direction.y][direction.x]
|
||||
if (!isNaN(char)) {
|
||||
let re = new RegExp("(([0-9]+)?" + char + "([0-9]+)?)", "gm")
|
||||
chars.push(parseInt([...lines[direction.y].matchAll(re)].find(number => direction.x <= (number.index + number[0].length - 1))[0]))
|
||||
}
|
||||
})
|
||||
chars = [...new Set(chars)]
|
||||
return chars
|
||||
}
|
||||
function getNumberFromLine(line) {
|
||||
"".match()
|
||||
}
|
||||
function getDirections(x, y) {
|
||||
const directions = [...new Set([
|
||||
{ x: Math.max(0, x - 1), y: Math.max(0, y - 1) },
|
||||
{ x: x, y: Math.max(0, y - 1) },
|
||||
{ x: Math.min(lines[0].length - 1, x + 1), y: Math.max(0, y - 1) },
|
||||
{ x: Math.max(0, x - 1), y: y },
|
||||
{ x: Math.min(lines[0].length - 1, x + 1), y: y },
|
||||
{ x: Math.max(0, x - 1), y: Math.min(lines.length - 1, y + 1) },
|
||||
{ x: x, y: Math.min(lines.length - 1, y + 1) },
|
||||
{ x: Math.min(lines[0].length - 1, x + 1), y: Math.min(lines.length - 1, y + 1) }
|
||||
])]
|
||||
return directions
|
||||
}
|
||||
|
||||
// -----
|
||||
|
||||
function findStructure(lines, x, y) {
|
||||
if (!lines) return false
|
||||
let result = structureMap.find(coords => coords.x === x && coords.y === y) ? true : false
|
||||
|
Reference in New Issue
Block a user