Untitled
unknown
javascript
4 years ago
1.5 kB
8
Indexable
const fs = require('fs'); const lines = fs.readFileSync('input.txt').toString().split("\n").filter(v => v).map(x => x.split(' -> ')).reduce((acc, [point, point2], idx) => { const [x1, y1] = point.split(',').map(Number); const [x2, y2] = point2.split(',').map(Number); acc.push({ x1, y1, x2, y2, }); return acc; }, []) const computeRes = (lines = [], filterFunction = () => true) => { const intersections = new Map(); lines .filter(filterFunction) .forEach(({ x1, y1, x2, y2 }) => { let moveX; let moveY; if (x1 === x2) { moveX = 0; } if (x1 < x2) { moveX = 1; } if (x1 > x2) { moveX = -1; } if (y1 === y2) { moveY = 0; } if (y1 < y2) { moveY = 1; } if (y1 > y2) { moveY = -1; } do { const pointValue = intersections.get(`${x1},${y1}`) || 0; intersections.set(`${x1},${y1}`, pointValue + 1); x1 += moveX; y1 += moveY; } while (x1 !== x2 + moveX || y1 !== y2 + moveY); }) let intersectionsAboveTwo = 0; for (let [_, value] of intersections) { if (value >= 2) { intersectionsAboveTwo++; } } return intersectionsAboveTwo; } //a console.log(computeRes(lines, ({ x1, x2, y1, y2 }) => (x1 === x2 || y1 === y2))); //b console.log(computeRes(lines));
Editor is loading...