Advent of Code 2022 - Day 8: Treetop Tree House Solution
Dealing with matrix, straight-forwardPart 1
Let the code speak for itself
Implementation
1const fs = require("fs")23const readData = () => {4 const data = fs5 .readFileSync("./input", "utf-8")6 .split(/\r?\n/)7 .map(row => row.split(""))89 return data10}1112const main = () => {13 const matrix = readData()1415 const [numberOfRows, numberOfCols] = [matrix.length, matrix[0].length]1617 let res = 01819 const isEdge = (row, col) =>20 row === 0 ||21 col === 0 ||22 row === numberOfRows - 1 ||23 col === numberOfCols - 12425 const isVisible = (row, col) => {26 if (isEdge(row, col)) return true2728 const isValid = cellValue => cellValue < matrix[row][col]2930 const [rowValues, colValues] = [31 matrix[row],32 Array.from({ length: numberOfRows }, (_, i) => matrix[i][col]),33 ]3435 return [36 rowValues.slice(0, col).every(isValid),37 rowValues.slice(col + 1).every(isValid),38 colValues.slice(0, row).every(isValid),39 colValues.slice(row + 1).every(isValid),40 ].some(Boolean)41 }4243 for (let row = 0; row < numberOfRows; row++) {44 for (let col = 0; col < numberOfCols; col++) {45 res += isVisible(row, col)46 }47 }4849 console.log(res)50}5152main()
Part 2
Early-break loop and count
Implementation
1const fs = require("fs")23const readData = () => {4 const data = fs5 .readFileSync("./input", "utf-8")6 .split(/\r?\n/)7 .map(row => row.split(""))89 return data10}1112const main = () => {13 const matrix = readData()1415 const [numberOfRows, numberOfCols] = [matrix.length, matrix[0].length]1617 let res = Number.NEGATIVE_INFINITY1819 const isEdge = (row, col) =>20 row === 0 ||21 col === 0 ||22 row === numberOfRows - 1 ||23 col === numberOfCols - 12425 const calculateScenicScore = (row, col) => {26 if (isEdge(row, col)) return 02728 const scoreAccumulator = ({ count, stop }, el) => {29 if (stop) return { count, stop }3031 stop = el >= matrix[row][col]32 count += 13334 return { count, stop }35 }3637 const [rowValues, colValues] = [38 matrix[row],39 Array.from({ length: numberOfRows }, (_, i) => matrix[i][col]),40 ]4142 return [43 rowValues44 .slice(0, col)45 .reverse()46 .reduce(scoreAccumulator, { count: 0, stop: false }).count,47 rowValues48 .slice(col + 1)49 .reduce(scoreAccumulator, { count: 0, stop: false }).count,50 colValues51 .slice(0, row)52 .reverse()53 .reduce(scoreAccumulator, { count: 0, stop: false }).count,54 colValues55 .slice(row + 1)56 .reduce(scoreAccumulator, { count: 0, stop: false }).count,57 ].reduce((acc, el) => acc * el, 1)58 }5960 for (let row = 0; row < numberOfRows; row++) {61 for (let col = 0; col < numberOfCols; col++) {62 res = Math.max(res, calculateScenicScore(row, col))63 }64 }6566 console.log(res)67}6869main()
References
Comments
Loading comments...
Tags
adventofcode
matrix
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.