const fs = require('fs'); const readline = require('readline'); function isVisible(map, posX, posY) { let i =1; let visibility = [ true, true, true, true ]; let maxDist = [ posX, map.length -posX -1, posY, map[0].length -posY -1 ]; let cross = []; do { cross = [ map[posX-i]?.[posY], map[posX+i]?.[posY], map[posX]?.[posY-i], map[posX]?.[posY+i] ]; cross.forEach((tree, index) => { if (tree && tree.height >= map[posX][posY].height && visibility[index]) { visibility[index] = false; maxDist[index] = i; } }); ++i; } while (!cross.every(x => !x) && !visibility.every(i => !i)); map[posX][posY].visible = visibility.filter(i => !!i).length; map[posX][posY].maxDist = maxDist.reduce((acc, i) => acc ? acc * i : i); } function getColor(height) { return "\033[" +[39, 32, 32, 92, 33, 93, 93, 91, 31, 31][height] +'m'; } async function main() { let lines = []; let visible = []; for await (let line of readline.createInterface({ input: process.stdin })) { lines.push(line.split("").map(i => { return { height: parseInt(i), visible: 0, maxDist: 0 };})); } for (let i =0; i < lines.length; ++i) for (let j =0; j < lines[i].length; ++j) isVisible(lines, i, j); for (let i of lines) { let buf = ""; for (let j of i) buf += getColor(j.height) +'x'; buf += getColor(0); console.log(buf); } console.log("Number of visible trees: ", lines.reduce((acc, i) => acc ? acc.concat(i) : i).filter(i => i.visible).length); console.log("Max scenic score: ", Math.max(...lines.reduce((acc, i) => acc ? acc.concat(i) : i).map(i => i.maxDist))); }; (main());