commit d66837d90c21187010e86d67bbe762d26cc88293
parent c17cc3ebd4a2040f9643bc15433edfcf37dece61
Author: finwo <finwo@pm.me>
Date: Thu, 8 Dec 2022 12:20:14 +0100
Added 2022/08 solution
Diffstat:
2 files changed, 131 insertions(+), 0 deletions(-)
diff --git a/2022/day-08/index.js b/2022/day-08/index.js
@@ -0,0 +1,126 @@
+const fs = require('fs');
+const through = require('through2');
+const char_for_char = require('../common/char-for-char.js');
+
+let row = [];
+const grid = [row];
+
+const rotate = arr => {
+ const out = [];
+ const row = [];
+ for(let x=0; x<arr[0].length; x++) {
+ out[x] = [];
+ for(let y=0; y<arr.length; y++) {
+ out[x][arr[0].length - 1 - y] = arr[y][x];
+ }
+ }
+ return out;
+};
+
+fs.createReadStream('input')
+// fs.createReadStream('input')
+ .pipe(char_for_char())
+
+ // Load grid
+ .pipe(through(function(char, enc, cb) {
+ char = char.toString();
+
+ // Newline = row separator
+ if (char == '\n') {
+ grid.push(row = []);
+ return cb();
+ }
+
+ row.push(parseInt(char));
+ cb();
+ }))
+
+ .on('finish', () => {
+ // done
+
+ // Remove last row if it's empty
+ // Row is still a reference to it
+ if (row.length == 0) {
+ grid.pop();
+ }
+
+ // START PART 1
+
+ // Make perspectives of the grid
+ // const grids = [[[0,1],[2,3]]]; // Test rotation
+ const grids = [rotate(grid)];
+ grids.push(rotate(grids[grids.length-1]));
+ grids.push(rotate(grids[grids.length-1]));
+ grids.push(rotate(grids[grids.length-1]));
+
+ // Check every perspective from left to right
+ for(const perspective of grids) {
+ for(let y=0; y<perspective.length; y++) {
+ let colH = -1;
+ for(let x=0; x<perspective.length; x++) {
+ if (perspective[y][x] > colH) {
+ colH = perspective[y][x];
+ perspective[y][x] = 1;
+ } else {
+ perspective[y][x] = 0;
+ }
+ }
+ }
+ }
+
+ // Turn perspectives upright again
+ for(let x=0; x<1; x++) grids[2] = rotate(grids[2]);
+ for(let x=0; x<2; x++) grids[1] = rotate(grids[1]);
+ for(let x=0; x<3; x++) grids[0] = rotate(grids[0]);
+
+ // Merge perspectives
+ // No need to rotate, just using it as initializer
+ let merged = rotate(grid);
+ for(const row of merged) {
+ for(let x=0; x<row.length; x++) row[x] = 0;
+ }
+ for(const perspective of grids) {
+ for(let y=0; y<perspective.length; y++) {
+ for(let x=0; x<perspective.length; x++) {
+ merged[y][x] = merged[y][x] || perspective[y][x];
+ }
+ }
+ }
+
+ // Count the visible trees
+ const visible = merged.reduce((r,a) => r + a.reduce((r, a) => r+a, 0), 0);
+ console.log({ visible });
+
+
+ // START PART 2
+
+ const views = [];
+
+ // Calculate scenic score for each position
+ for(let y=0; y<grid.length; y++) {
+ views[y] = [];
+ for(let x=0; x<grid.length; x++) {
+ views[y][x] = [];
+ let v=0;
+
+ // Direction: up, left, down, right
+ v=0; for(let i=y-1; i>=0; i--) { v++; if (grid[i][x] >= grid[y][x]) { break; } } views[y][x].push(v);
+ v=0; for(let i=x-1; i>=0; i--) { v++; if (grid[y][i] >= grid[y][x]) { break; } } views[y][x].push(v);
+ v=0; for(let i=y+1; i<grid.length; i++) { v++; if (grid[i][x] >= grid[y][x]) { break; } } views[y][x].push(v);
+ v=0; for(let i=x+1; i<grid.length; i++) { v++; if (grid[y][i] >= grid[y][x]) { break; } } views[y][x].push(v);
+
+ // "Scenic score" = multiply house's views together
+ views[y][x] = views[y][x].reduce((r, a) => r*a, 1);
+ }
+ }
+
+ // Find the highest score
+ let highscore = 0;
+ for(let y=0; y<grid.length; y++) {
+ for(let x=0; x<grid.length; x++) {
+ if (views[y][x] >= highscore) highscore = views[y][x];
+ }
+ }
+
+ console.log({ highscore });
+ });
diff --git a/2022/day-08/sample b/2022/day-08/sample
@@ -0,0 +1,5 @@
+30373
+25512
+65332
+33549
+35390