Created
December 8, 2022 14:38
-
-
Save BiosBoy/237049b0956f7a0c50fb32b9a789a5c1 to your computer and use it in GitHub Desktop.
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 fs = require('fs'); | |
const path = require('path'); | |
const data = fs.readFileSync(path.join(__dirname, './input8.txt'), 'utf8'); | |
const arr = data.split('\n').map(item => item.split('')); | |
const treesBoxCount = 16; | |
const trees = [...Array.from(Array(arr.length - 1).keys()).map(() => [])]; | |
const config = { | |
left: 0, | |
right: 0, | |
bottom: 0, | |
top: 0 | |
} | |
const checkAlreadyAdded = (elem, treeId) => { | |
const isAlreadyAdded = trees[treeId - 1].some(row => row.idx === elem.idx); | |
return isAlreadyAdded; | |
} | |
const checkVisibility = ({ elem, iteration, treeId, visible, condition, debug }) => { | |
const tree = arr[treeId]; | |
debug && console.log(elem, iteration, 'START'); | |
if (visible === 'hidden' || checkAlreadyAdded(elem, treeId)) { | |
return; | |
} | |
if (iteration !== 0) { | |
const isElemVisible = condition(iteration - 1, elem, tree, debug); | |
debug && console.log(isElemVisible, 'isElemVisible'); | |
return checkVisibility({ elem, iteration: iteration - 1, treeId, visible: isElemVisible ? 'visible' : 'hidden', condition, debug }); | |
} | |
debug && console.log(elem, 'TEST!!') | |
visible && trees[treeId - 1].push(elem); | |
} | |
arr.forEach((item, idx) => { | |
// do not run for the top/bottom borders | |
if (idx === 0 || (idx === arr.length - 1)) { | |
return | |
} | |
item.forEach((innerItem, innerIdx) => { | |
// do not run for the left/right borders | |
if (innerIdx === 0 || (innerIdx === item.length - 1)) { | |
return | |
} | |
config.left = innerIdx; | |
config.right = (item.length - 1) - innerIdx; | |
config.bottom = (arr.length - 1) - idx; | |
config.top = idx; | |
const elem = { idx: innerIdx, item: innerItem }; | |
// left | |
checkVisibility({ | |
condition: (itr, elemItem, arrTo) => elemItem.item > arrTo[itr], | |
iteration: config.left, | |
treeId: idx, | |
elem | |
}); | |
// right | |
checkVisibility({ | |
condition: (itr, elemItem, arrTo) => elemItem.item > arrTo[arrTo.length - (itr + 1)], | |
iteration: config.right, | |
treeId: idx, | |
elem | |
}); | |
// top | |
checkVisibility({ | |
condition: (itr, elemItem) => elemItem.item > arr[itr][innerIdx], | |
iteration: config.top, | |
treeId: idx, | |
elem | |
}); | |
// bottom | |
checkVisibility({ | |
condition: (itr, elemItem) => elemItem.item > arr[arr.length - (itr + 1)][innerIdx], | |
iteration: config.bottom, | |
treeId: idx, | |
elem | |
}); | |
}); | |
}); | |
const part1 = trees.flatMap(num => num).length; | |
console.log(part1 + treesBoxCount, 'arr'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment