advent-of-code/2022/15/index.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-12-15 08:07:09 +00:00
const fs = require('fs/promises');
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
const arrayOfLength = n => Array.from(Array(n).keys()).map(() => {});
const parseXY = str => str.match(/x=(.*), y=(.*)$/).slice(1).map(x => BigInt(x));
const ROW = 2000000n;
const abs = x => x < 0n ? x * -1n : x;
const main = async () => {
const input = (await fs.readFile("./input.txt", 'utf8')).split("\n").slice(0, -1);
console.log(input);
const sets = input.map(row => row.split(": ").map(parseXY));
const map = new Set();
const getDistance = (x1, y1, x2, y2) => abs(x1 - x2) + abs(y1 - y2);
const fillRow = (centerX, radius) => {
console.log("Fill radius: ", radius);
for(let i = centerX - radius; i <= centerX + radius; ++i) {
map.add(i);
}
}
for(let [[x, y], b] of sets) {
const radius = getDistance(x, y, ...b);
const halfWidth = radius - abs(ROW - y)
if(halfWidth > 0) {
fillRow(x, halfWidth);
}
}
for(let [_, b] of sets) {
if(b[1] === ROW) {
map.delete(b[0]);
}
}
console.log("ANSWER: ", map.size);
}
main();