|
| 1 | +import fs from "fs" |
| 2 | + |
| 3 | +const readInput = fs.readFileSync(__dirname + '/input.txt', 'utf-8'); |
| 4 | + |
| 5 | +const formattedInput = readInput.trim().split('\n'); |
| 6 | + |
| 7 | +const formatEntry = (entry) => { |
| 8 | + const line = entry.split(' -> ') |
| 9 | + |
| 10 | + const startingPoint = line[0].trim().split(',').map(x => Number(x)) |
| 11 | + const endPoint = line[1].trim().split(',').map(y => Number(y)) |
| 12 | + |
| 13 | + return [startingPoint, endPoint] |
| 14 | +} |
| 15 | + |
| 16 | +const hasCommonRoot = (start, end) => { |
| 17 | + if (start[0] === end[0] || start[1] === end[1]) { |
| 18 | + return true |
| 19 | + } |
| 20 | + return false |
| 21 | +} |
| 22 | + |
| 23 | +const checkUncommonIndex = (start, end) => { |
| 24 | + if (start[0] !== end[0]) { |
| 25 | + return 0 |
| 26 | + } |
| 27 | + return 1 |
| 28 | +} |
| 29 | + |
| 30 | +const findLargest = (arr) => { |
| 31 | + let largest = 0; |
| 32 | + for (let i = 0; i < arr.length; i++) { |
| 33 | + if (largest < arr[i] ) { |
| 34 | + largest = arr[i]; |
| 35 | + } |
| 36 | + } |
| 37 | + return largest |
| 38 | +} |
| 39 | + |
| 40 | +const getGridRange = (input) => { |
| 41 | + let xEntries = []; |
| 42 | + let yEntries = []; |
| 43 | + |
| 44 | + for (let entry of input) { |
| 45 | + const [begin, stop] = formatEntry(entry) |
| 46 | + |
| 47 | + xEntries.push(begin[0]) |
| 48 | + yEntries.push(begin[1]) |
| 49 | + xEntries.push(stop[0]) |
| 50 | + yEntries.push(stop[1]) |
| 51 | + } |
| 52 | + |
| 53 | + const xMax = findLargest(xEntries) |
| 54 | + const yMax = findLargest(yEntries) |
| 55 | + |
| 56 | + return [xMax, yMax] |
| 57 | +} |
| 58 | + |
| 59 | +export const countPointsAboveTwo = (lines) => { |
| 60 | + |
| 61 | + const [x, y] = getGridRange(lines) |
| 62 | + |
| 63 | + const grid = Array(x + 1).fill().map(() => Array(y + 1).fill(0)) |
| 64 | + |
| 65 | + for (let lineValue of lines) { |
| 66 | + const [start, end] = formatEntry(lineValue) |
| 67 | + |
| 68 | + if (!hasCommonRoot(start, end)) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + const index = checkUncommonIndex(start, end) |
| 73 | + |
| 74 | + const commonIndex = index === 1 ? 0 : 1 |
| 75 | + |
| 76 | + let startIndex = Math.min(start[index], end[index]) |
| 77 | + let endIndex = Math.max(start[index], end[index]) |
| 78 | + |
| 79 | + for (let i = startIndex; i <= endIndex; i++) { |
| 80 | + |
| 81 | + if (commonIndex === 1) { |
| 82 | + grid[start[commonIndex]][i] += 1 |
| 83 | + } else { |
| 84 | + grid[i][start[commonIndex]] += 1 |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + const flattenGrid = grid.reduce( |
| 91 | + (previousValue, currentValue) => previousValue.concat(currentValue), |
| 92 | + [] |
| 93 | + ) |
| 94 | + |
| 95 | + const twoOrGreater = flattenGrid.filter( entry => Number(entry) >= 2 ) |
| 96 | + |
| 97 | + return twoOrGreater.length |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +export const countPointsAboveTwoDiagonal = (lines) => { |
| 102 | + |
| 103 | + const [x, y] = getGridRange(lines) |
| 104 | + |
| 105 | + const grid = Array(x + 1).fill().map(() => Array(y + 1).fill(0)) |
| 106 | + |
| 107 | + for (let lineValue of lines) { |
| 108 | + const [start, end] = formatEntry(lineValue) |
| 109 | + |
| 110 | + if (hasCommonRoot(start, end)) { |
| 111 | + const index = checkUncommonIndex(start, end) |
| 112 | + |
| 113 | + const commonIndex = index === 1 ? 0 : 1 |
| 114 | + |
| 115 | + let startIndex = Math.min(start[index], end[index]) |
| 116 | + let endIndex = Math.max(start[index], end[index]) |
| 117 | + |
| 118 | + for (let i = startIndex; i <= endIndex; i++) { |
| 119 | + |
| 120 | + if (commonIndex === 1) { |
| 121 | + grid[start[commonIndex]][i] += 1 |
| 122 | + } else { |
| 123 | + grid[i][start[commonIndex]] += 1 |
| 124 | + } |
| 125 | + } |
| 126 | + } else { |
| 127 | + const diff = Math.abs(start[0] - end[0]) |
| 128 | + |
| 129 | + for (let j = 0; j <= diff; j++) { |
| 130 | + if (start[0] < end[0] && start[1] < end[1]) { |
| 131 | + grid[start[1] + j][start[0] + j] += 1 |
| 132 | + } else if (start[0] > end[0] && start[1] > end[1]) { |
| 133 | + grid[start[1] - j][start[0] - j] += 1 |
| 134 | + } else if (start[0] < end[0] && start[1] > end[1]) { |
| 135 | + grid[start[1] - j][start[0] + j] += 1 |
| 136 | + } else if (start[0] > end[0] && start[1] < end[1]) { |
| 137 | + grid[start[1] + j][start[0] - j] += 1 |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + const flattenGrid = grid.reduce( |
| 144 | + (previousValue, currentValue) => previousValue.concat(currentValue), |
| 145 | + [] |
| 146 | + ) |
| 147 | + |
| 148 | + const twoOrGreater = flattenGrid.filter( entry => Number(entry) >= 2 ) |
| 149 | + |
| 150 | + return twoOrGreater.length |
| 151 | +} |
| 152 | + |
| 153 | +console.log("The count of points greater than 1 is: ", countPointsAboveTwo(formattedInput)) |
| 154 | +console.log("The count of points greater than 1 including diagonals is: ", countPointsAboveTwoDiagonal(formattedInput)) |
0 commit comments