Created
December 8, 2022 16:18
-
-
Save acorn1010/449d9cf072da17742557fd6195a4d29b to your computer and use it in GitHub Desktop.
AoC Day 8
This file contains hidden or 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
import * as fs from "fs"; | |
function readFile(path: number) { | |
const result = | |
fs.readFileSync(`/home/acorn/projects/js/foony/scripts/src/aoc/${path}.txt`, 'utf-8').split('\r\n'); | |
for (let i = result.length - 1; i >= 0; --i) { | |
if (!result[i].trim()) { | |
--result.length; // Last line is empty, remove it | |
} else { | |
return result; | |
} | |
} | |
return result; | |
} | |
function eight() { | |
const grid: number[][] = []; //[y][x] | |
function isVisible(x: number, y: number) { | |
const height = grid[y][x]; | |
if (x === 0 || x === grid[y].length - 1 || y === 0 || y === grid.length - 1) { | |
return true; | |
} | |
for (let i = x - 1; i >= 0; --i) { // left | |
if (height <= grid[y][i]) { | |
break; | |
} | |
if (i === 0) { | |
return true; | |
} | |
} | |
for (let i = x + 1; i < grid[y].length; ++i) { // Right | |
if (height <= grid[y][i]) { | |
break; | |
} | |
if (i === grid[y].length - 1) { | |
return true; | |
} | |
} | |
for (let j = y - 1; j >= 0; --j) { // Top | |
if (height <= grid[j][x]) { | |
break; | |
} | |
if (j === 0) { | |
return true; | |
} | |
} | |
for (let j = y + 1; j < grid.length; ++j) { // Bottom | |
if (height <= grid[j][x]) { | |
break; | |
} | |
if (j === grid.length - 1) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function calculateScore(x: number, y: number) { | |
const height = grid[y][x]; | |
let score = 1; | |
let multiplier = 0; | |
for (let i = x - 1; i >= 0; --i) { // left | |
++multiplier; | |
if (height <= grid[y][i]) { | |
break; | |
} | |
} | |
score *= multiplier; | |
multiplier = 0; | |
for (let i = x + 1; i < grid[y].length; ++i) { // Right | |
++multiplier; | |
if (height <= grid[y][i]) { | |
break; | |
} | |
} | |
score *= multiplier; | |
multiplier = 0; | |
for (let j = y - 1; j >= 0; --j) { // Top | |
++multiplier; | |
if (height <= grid[j][x]) { | |
break; | |
} | |
} | |
score *= multiplier; | |
multiplier = 0; | |
for (let j = y + 1; j < grid.length; ++j) { // Bottom | |
++multiplier; | |
if (height <= grid[j][x]) { | |
break; | |
} | |
} | |
score *= multiplier; | |
return score; | |
} | |
for (const line of readFile(8)) { | |
if (line) { | |
grid.push(line.split('').map(line => +line)); | |
} | |
} | |
let maxScore = 0; | |
for (let y = 0; y < grid.length; ++y) { | |
for (let x = 0; x < grid[y].length; ++x) { | |
console.log({x, y, score: calculateScore(x, y)}); | |
maxScore = Math.max(maxScore, calculateScore(x, y)); | |
} | |
} | |
console.log('total score', maxScore); | |
} | |
eight(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment