Advent of Code 2022 - Day 8: Treetop Tree House Solution

Dealing with matrix, straight-forward

Part 1

Let the code speak for itself

Implementation

1const fs = require("fs")
2
3const readData = () => {
4 const data = fs
5 .readFileSync("./input", "utf-8")
6 .split(/\r?\n/)
7 .map(row => row.split(""))
8
9 return data
10}
11
12const main = () => {
13 const matrix = readData()
14
15 const [numberOfRows, numberOfCols] = [matrix.length, matrix[0].length]
16
17 let res = 0
18
19 const isEdge = (row, col) =>
20 row === 0 ||
21 col === 0 ||
22 row === numberOfRows - 1 ||
23 col === numberOfCols - 1
24
25 const isVisible = (row, col) => {
26 if (isEdge(row, col)) return true
27
28 const isValid = cellValue => cellValue < matrix[row][col]
29
30 const [rowValues, colValues] = [
31 matrix[row],
32 Array.from({ length: numberOfRows }, (_, i) => matrix[i][col]),
33 ]
34
35 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 }
42
43 for (let row = 0; row < numberOfRows; row++) {
44 for (let col = 0; col < numberOfCols; col++) {
45 res += isVisible(row, col)
46 }
47 }
48
49 console.log(res)
50}
51
52main()

Part 2

Early-break loop and count

Implementation

1const fs = require("fs")
2
3const readData = () => {
4 const data = fs
5 .readFileSync("./input", "utf-8")
6 .split(/\r?\n/)
7 .map(row => row.split(""))
8
9 return data
10}
11
12const main = () => {
13 const matrix = readData()
14
15 const [numberOfRows, numberOfCols] = [matrix.length, matrix[0].length]
16
17 let res = Number.NEGATIVE_INFINITY
18
19 const isEdge = (row, col) =>
20 row === 0 ||
21 col === 0 ||
22 row === numberOfRows - 1 ||
23 col === numberOfCols - 1
24
25 const calculateScenicScore = (row, col) => {
26 if (isEdge(row, col)) return 0
27
28 const scoreAccumulator = ({ count, stop }, el) => {
29 if (stop) return { count, stop }
30
31 stop = el >= matrix[row][col]
32 count += 1
33
34 return { count, stop }
35 }
36
37 const [rowValues, colValues] = [
38 matrix[row],
39 Array.from({ length: numberOfRows }, (_, i) => matrix[i][col]),
40 ]
41
42 return [
43 rowValues
44 .slice(0, col)
45 .reverse()
46 .reduce(scoreAccumulator, { count: 0, stop: false }).count,
47 rowValues
48 .slice(col + 1)
49 .reduce(scoreAccumulator, { count: 0, stop: false }).count,
50 colValues
51 .slice(0, row)
52 .reverse()
53 .reduce(scoreAccumulator, { count: 0, stop: false }).count,
54 colValues
55 .slice(row + 1)
56 .reduce(scoreAccumulator, { count: 0, stop: false }).count,
57 ].reduce((acc, el) => acc * el, 1)
58 }
59
60 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 }
65
66 console.log(res)
67}
68
69main()

References

Original problem

Comments

Loading comments...

Tags

adventofcode

matrix

Next Post

Advent of Code 2022 - Day 9: Rope Bridge

Dec 9, 2022

Manipulate coordination

Previous Post

HoningJS

Search Posts