Created
December 10, 2019 15:10
-
-
Save cristijora/23d003ca0d9dd2dc6e922aa4a487e975 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const path = require('path') | |
const data = fs.readFileSync(path.join(__dirname, 'input.txt'), 'utf-8') | |
const rows = data.split('\n').filter(r => r !== '') | |
const matrix = rows.map(row => { | |
return row.split('').map(cell => { | |
if (cell === '.') { | |
return '.' | |
} | |
return 0 | |
}) | |
}) | |
function part1(matrix) { | |
let max = Number.MIN_SAFE_INTEGER | |
let maxPoint = {} | |
matrix.forEach((row, rowIndex) => { | |
row.forEach((cell, colIndex) => { | |
if (cell !== '.') { | |
let angleCount = computeAnglesForCell(matrix, { x: rowIndex, y: colIndex }) | |
matrix[rowIndex][colIndex] = angleCount | |
if (angleCount > max) { | |
max = angleCount | |
maxPoint = { x: rowIndex, y: colIndex } | |
} | |
} | |
}) | |
}) | |
return { | |
max, | |
maxPoint | |
} | |
} | |
function computeAnglesForCell(matrix, cellCoords) { | |
let anglesSet = new Set() | |
matrix.forEach((row, rowIndex) => { | |
row.forEach((cell, colIndex) => { | |
if (cell !== '.') { | |
const p1 = { x: rowIndex, y: colIndex } | |
let angle = getAngle(p1, cellCoords) | |
if (cellCoords.x === rowIndex && colIndex === cellCoords.y) { | |
// skip same cell | |
} else { | |
anglesSet.add(angle) | |
} | |
} | |
}) | |
}) | |
return anglesSet.size | |
} | |
function getAngle(p1, p2) { | |
return Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI; | |
} | |
let { maxPoint, max } = part1(matrix) | |
console.log(maxPoint, max) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment