|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const distance = require('manhattan'); |
| 4 | +const flat = require('array.prototype.flat'); |
| 5 | + |
| 6 | +let raw_input = fs.readFileSync(path.resolve(__dirname, './input.txt'), 'utf8'); |
| 7 | + |
| 8 | +// Last filter is to remove any empty lines |
| 9 | +let input = raw_input.split('\n').filter(n => n); |
| 10 | + |
| 11 | +let coordinates = input.map(p => p.split(',').map(n => +n)); |
| 12 | + |
| 13 | +let flattened = flat(coordinates); |
| 14 | +let largest = Math.max.apply(null, flattened); |
| 15 | + |
| 16 | +let grid = Array(largest + 2) |
| 17 | + .fill() |
| 18 | + .map(n => { |
| 19 | + return Array(largest + 2).fill(-1); |
| 20 | + }); |
| 21 | + |
| 22 | +// Loop through grid and mark spots that are coordinates |
| 23 | +coordinates.forEach((coord, index) => { |
| 24 | + let [i, j] = coord; |
| 25 | + grid[i][j] = 'C' + index; |
| 26 | +}); |
| 27 | + |
| 28 | +// Iterate each coord in grid, and measure its distance from each point in our list. |
| 29 | +// Keep track of the smallest one. If tie, mark as null |
| 30 | +for (let i = 0; i < grid.length; i++) { |
| 31 | + let row = grid[i]; |
| 32 | + for (let j = 0; j < grid.length; j++) { |
| 33 | + let cell = row[j]; |
| 34 | + if (typeof cell !== 'string') { |
| 35 | + // Not a coord, measure it |
| 36 | + let lengths = coordinates.map((coord, index) => { |
| 37 | + let d = distance(coord, [i, j]); |
| 38 | + return { |
| 39 | + index: index, |
| 40 | + dist: d, |
| 41 | + }; |
| 42 | + }); |
| 43 | + |
| 44 | + lengths.sort((a, b) => { |
| 45 | + if (a.dist < b.dist) return -1; |
| 46 | + else if (a.dist > b.dist) return 1; |
| 47 | + else return 0; |
| 48 | + }); |
| 49 | + |
| 50 | + let first = lengths[0]; |
| 51 | + let second = lengths[1]; |
| 52 | + |
| 53 | + if (first.dist === second.dist) { |
| 54 | + // tied distance, mark as null |
| 55 | + grid[i][j] = null; |
| 56 | + } else { |
| 57 | + grid[i][j] = first.index; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Now, get all edges |
| 64 | +let edges = []; |
| 65 | +for (let i = 0; i < grid.length; i++) { |
| 66 | + for (let dir = 0; dir < 4; dir++) { |
| 67 | + if (dir === 0) { |
| 68 | + // top row |
| 69 | + edges.push(grid[0][i]); |
| 70 | + } else if (dir === 1) { |
| 71 | + // right col |
| 72 | + edges.push(grid[i][grid.length - 1]); |
| 73 | + } else if (dir === 2) { |
| 74 | + // bottom row col |
| 75 | + edges.push(grid[grid.length - 1][i]); |
| 76 | + } else { |
| 77 | + // left col |
| 78 | + edges.push(grid[i][0]); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +let unique_edges_list = [...new Set(edges)]; |
| 84 | +unique_edges_list = unique_edges_list.filter(n => typeof n === 'number'); |
| 85 | +let unique_edges = {}; |
| 86 | +unique_edges_list.forEach(e => (unique_edges[e] = true)); |
| 87 | + |
| 88 | +let total_area = {}; |
| 89 | + |
| 90 | +// Loop through grid and remove all edge nodes (they are infinite area) |
| 91 | +for (let i = 0; i < grid.length; i++) { |
| 92 | + for (let j = 0; j < grid.length; j++) { |
| 93 | + let cell = grid[i][j]; |
| 94 | + if (typeof cell === 'number') { |
| 95 | + if (unique_edges[cell]) { |
| 96 | + // Remove any index with infinite area |
| 97 | + grid[i][j] = null; |
| 98 | + } else { |
| 99 | + if (!total_area[cell]) { |
| 100 | + total_area[cell] = 0; |
| 101 | + } |
| 102 | + |
| 103 | + total_area[cell] += 1; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +let areas = Object.entries(total_area); |
| 110 | +areas.sort((a, b) => { |
| 111 | + if (a[1] < b[1]) return -1; |
| 112 | + else if (a[1] > b[1]) return 1; |
| 113 | + else return 0; |
| 114 | +}); |
| 115 | + |
| 116 | +let max = areas.pop(); |
| 117 | + |
| 118 | +// Plus 1 because we need to count the original coordinate!! |
| 119 | +console.log(max[1] + 1); |
0 commit comments