9
This commit is contained in:
109
src/2022/day8/index.ts
Normal file
109
src/2022/day8/index.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import path from 'path';
|
||||
import { readFile, sum } from "../../utils";
|
||||
|
||||
let input = readFile(path.resolve(__dirname, 'input.txt'));
|
||||
|
||||
let grid: any[] = [];
|
||||
|
||||
input.split("\n").forEach(element => {
|
||||
const arr = element.split("");
|
||||
grid.push(arr);
|
||||
});
|
||||
|
||||
const checkVisible = (x: any, y: any) => {
|
||||
// Check up
|
||||
const treeVal = grid[y][x];
|
||||
|
||||
let stillVisible = true;
|
||||
for (let it = y - 1; it >= 0; it--) {
|
||||
if (grid[it][x] >= treeVal) stillVisible = false;
|
||||
}
|
||||
if (stillVisible) return true;
|
||||
|
||||
stillVisible = true;
|
||||
for (let it = y + 1; it < grid.length; it++) {
|
||||
if (grid[it][x] >= treeVal) stillVisible = false;
|
||||
}
|
||||
if (stillVisible) return true;
|
||||
|
||||
stillVisible = true;
|
||||
for (let it = x - 1; it >= 0; it--) {
|
||||
if (grid[y][it] >= treeVal) stillVisible = false;
|
||||
}
|
||||
if (stillVisible) return true;
|
||||
|
||||
stillVisible = true;
|
||||
for (let it = x + 1; it < grid.length; it++) {
|
||||
if (grid[y][it] >= treeVal) stillVisible = false;
|
||||
}
|
||||
if (stillVisible) return true;
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
let counter = 0;
|
||||
for (let x = 1; x < grid.length - 1; x++) {
|
||||
for (let y = 1; y < grid.length - 1; y++) {
|
||||
if (checkVisible(x, y)) counter++;
|
||||
}
|
||||
}
|
||||
|
||||
const edgeTrees = grid.length * 4 - 4;
|
||||
|
||||
console.log("Part 1:", counter + edgeTrees);
|
||||
|
||||
const getScenicScore = (x: any, y: any): number => {
|
||||
// Check up
|
||||
|
||||
|
||||
const treeVal = grid[y][x];
|
||||
let counter = 0;
|
||||
let score = 0;
|
||||
|
||||
for (let it = y - 1; it >= 0; it--) {
|
||||
counter++;
|
||||
if (grid[it][x] >= treeVal) break;
|
||||
}
|
||||
score = counter;
|
||||
|
||||
|
||||
counter = 0;
|
||||
for (let it = y + 1; it < grid.length; it++) {
|
||||
counter++;
|
||||
if (grid[it][x] >= treeVal) break;
|
||||
}
|
||||
|
||||
score = score * counter;
|
||||
|
||||
counter = 0;
|
||||
for (let it = x - 1; it >= 0; it--) {
|
||||
counter++;
|
||||
if (grid[y][it] >= treeVal) break;
|
||||
}
|
||||
|
||||
score = score * counter;
|
||||
|
||||
counter = 0;
|
||||
|
||||
for (let it = x + 1; it < grid.length; it++) {
|
||||
counter++;
|
||||
if (grid[y][it] >= treeVal) break;
|
||||
}
|
||||
|
||||
return score * counter;
|
||||
|
||||
// counter = 0;
|
||||
// return counter;
|
||||
|
||||
}
|
||||
|
||||
let scores: number[] = [];
|
||||
for (let x = 1; x < grid.length - 1; x++) {
|
||||
for (let y = 1; y < grid.length - 1; y++) {
|
||||
scores.push(getScenicScore(x,y));
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Part 2:" , Math.max(...scores));
|
||||
Reference in New Issue
Block a user