95 lines
2.2 KiB
TypeScript
95 lines
2.2 KiB
TypeScript
import { readFileSync } from 'fs';
|
|
import { join } from 'path';
|
|
|
|
const MINH = 0;
|
|
const MAXH = 9;
|
|
|
|
const getColumns = (arr, indices) => arr.map(row => indices.map(i => row[i])).reduce((accumulator, value) => accumulator.concat(value), []);
|
|
|
|
const fileString = readFileSync(join(__dirname, "input"), 'utf-8');
|
|
var rows = fileString.split("\n");
|
|
|
|
rows = rows.map(val => {
|
|
return val.trim();
|
|
});
|
|
rows = rows.filter(val => {
|
|
return val.length > 0;
|
|
});
|
|
|
|
const gridR = rows.map(val => {
|
|
return val.split("");
|
|
});
|
|
|
|
const grid = gridR.map(val => {
|
|
return val.map(vval => {
|
|
return +vval;
|
|
});
|
|
});
|
|
|
|
var visible = 0;
|
|
|
|
for (var i = 0; i < grid.length; i++) {
|
|
for (var j = 0; j < grid[i].length; j++) {
|
|
const tree = grid[i][j];
|
|
if (i - 1 < 0) { // Top of grid
|
|
visible += 1;
|
|
continue;
|
|
}
|
|
if (i + 1 >= grid.length) { // Bottom of grid
|
|
visible += 1;
|
|
continue;
|
|
}
|
|
if (j - 1 < 0) { // Left of grid
|
|
visible += 1;
|
|
continue;
|
|
}
|
|
if (j + 1 >= grid[i].length) { // Right of grid
|
|
visible += 1;
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
(tree > Math.max(...grid[i].slice(0, j))) || // Check Left in row
|
|
(tree > Math.max(...grid[i].slice(j + 1))) || // Check Left in row
|
|
(tree > Math.max(...getColumns(grid, [j]).slice(0, i))) || // Check Left in row
|
|
(tree > Math.max(...getColumns(grid, [j]).slice(i + 1))) // Check Left in row
|
|
) {
|
|
visible += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(visible);
|
|
|
|
var maxScore = 0;
|
|
for (var i = 0; i < grid.length; i++) {
|
|
for (var j = 0; j < grid[i].length; j++) {
|
|
const tree = grid[i][j];
|
|
|
|
const left = grid[i].slice(0, j).reverse();
|
|
const right = grid[i].slice(j + 1);
|
|
const top = getColumns(grid, [j]).slice(0, i).reverse();
|
|
const bot = getColumns(grid, [j]).slice(i + 1);
|
|
const checks = [left, right, top, bot];
|
|
var scores = [];
|
|
|
|
for (const check of checks) {
|
|
var cscore = 0;
|
|
for (var k = 0; k < check.length; k++) {
|
|
cscore += 1;
|
|
if (tree <= check[k]) {
|
|
break;
|
|
}
|
|
}
|
|
scores.push(cscore);
|
|
}
|
|
const score = scores.reduce( (a,b) => a * b );
|
|
|
|
if (score > maxScore) {
|
|
maxScore = score;
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(maxScore);
|