Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { readFileSync } from "fs";
- const dataSet = readFileSync(`${__dirname}/inputs/day5.txt`, "utf8").replace(/\r\n/g,'\n').split("\n")
- let SIZE = 0;
- const points = dataSet.map(d => {
- const [a, b] = d.split(" -> ")
- .map(s => {
- const [x, y] = s.split(",")
- return { x: +x, y: +y };
- });
- const size = Math.max(a.x, a.y, b.x, b.y) + 1;
- if (size > SIZE) {
- SIZE = size;
- }
- return [a, b];
- });
- const grid = new Array(SIZE).fill(0).map(() => new Array(SIZE).fill(0));
- for (const [a, b] of points) {
- const d = (b.y - a.y) / (b.x - a.x);
- if (Math.abs(d) === Infinity) {
- const [start, end] = [a.y, b.y].sort((r, t) => r - t);
- for (let y = start; y <= end; y += 1) {
- grid[y][a.x] += 1;
- }
- continue;
- }
- const [start, end] = [a.x, b.x].sort((r, t) => r - t);
- for (let x = start; x <= end; x += 1) {
- grid[d*(x - a.x) + a.y][x] += 1;
- }
- }
- const sum = grid.flat().reduce((s, x) => x > 1 ? s + 1 : s, 0)
- console.log("SUM", sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement