Home > front end >  Writing coordinate lines onto a 2D array representing a grid
Writing coordinate lines onto a 2D array representing a grid

Time:12-12

I'm trying to solve the following code challenge from advent of code: problem description

Essentially, I'm receiving input in the form of lines representing coordinate lines on a grid, i.e.: x1,y1 -> x2,y2. For the first part of the problem, I'm to draw these lines into a 2D array, then count the number of coordinates in the grid where lines have overlapped at least once. So far I've gotten to the point where I've successfully isolated which lines are valid (horizontal or vertical), and am trying to simply draw them onto the 2D array. For some bizarre reason that I can't pinpoint, my method is drawing only some of the lines onto the array, even though the lines are all identified as being valid that should be and are passed into the same code block that should be drawing the lines into the array. See the following code:

import { stdout } from 'process';

function getPuzzleInput (fileName : string) : [any[], number, number] {
    const fs = require('fs')
    let coordLines = [];
    let maxX = 0;
    let maxY = 0;

    try {
        const data : string = fs.readFileSync(fileName, 'utf8')
        const lines = data.split("\n");
        let insertIndex = 0;
        for (let lineNum=0; lineNum < lines.length; lineNum  )  {
            let coords : string[] = lines[lineNum].split(" -> ");
            let x1 : number = parseInt(coords[0].split(",")[0].trim());
            let y1 : number = parseInt(coords[0].split(",")[1].trim());
            let x2 : number = parseInt(coords[1].split(",")[0].trim());
            let y2 : number = parseInt(coords[1].split(",")[1].trim());

            if (x1 == x2 || y1 == y2) {
                coordLines.push({x1: x1, y1: y1, x2: x2, y2: y2});
                maxX = Math.max(maxX, x1, x2);
                maxY = Math.max(maxY, y1, y2);
            }
        }
    } catch (err) {
        console.error(err)
    }
    return [coordLines, maxX, maxY];
}

function getSolutionOne(coordLines, maxX : number, maxY : number) : void {
    let grid = [];
    for (let y = 0; y <= maxY; y  ) {
        grid[y] = [];
        for (let x = 0; x <= maxX; x  ) {
            grid[y][x] = 0;
        }
    }
    for (let line of coordLines) {
        if (line.x1 == line.x2) {
            console.log("Write into grid: ", line);
            // Write line into grid
            for (let y = line.y1; y <= line.y2; y  ) {
                grid[y][line.x1]  ;
            }
        } else if (line.y1 == line.y2) {
            console.log("Write into grid: ", line);
            // Write line into grid
            for (let x = line.x1; x <= line.x2; x  ) {
                grid[line.y1][x]  ;
            }
        }
    }
    printGridMap(grid, maxX, maxY);
}

function printGridMap(grid: any[], maxX : number, maxY : number) {
    stdout.write("x  0 1 2 3 4 5 6 7 8 9\n");
    stdout.write("----------------------\n");
    for (let y = 0; y <= maxY; y  ) {
        stdout.write(y.toString()   "| ");
        for (let x = 0; x <= maxX; x  ) {
            stdout.write(grid[y][x].toString()   " ");
        }
        stdout.write("\n");
    }
}

const main = () => {
    const [coordLines, maxX, maxY] = getPuzzleInput('./day5EX.txt');
    getSolutionOne(coordLines, maxX, maxY);
}

main();

This code results in the following to be outputted:

Write into grid:  { x1: 0, y1: 9, x2: 5, y2: 9 }
Write into grid:  { x1: 9, y1: 4, x2: 3, y2: 4 }
Write into grid:  { x1: 2, y1: 2, x2: 2, y2: 1 }
Write into grid:  { x1: 7, y1: 0, x2: 7, y2: 4 }
Write into grid:  { x1: 0, y1: 9, x2: 2, y2: 9 }
Write into grid:  { x1: 3, y1: 4, x2: 1, y2: 4 }
x  0 1 2 3 4 5 6 7 8 9
----------------------
0| 0 0 0 0 0 0 0 1 0 0 
1| 0 0 0 0 0 0 0 1 0 0 
2| 0 0 0 0 0 0 0 1 0 0 
3| 0 0 0 0 0 0 0 1 0 0 
4| 0 0 0 0 0 0 0 1 0 0 
5| 0 0 0 0 0 0 0 0 0 0 
6| 0 0 0 0 0 0 0 0 0 0 
7| 0 0 0 0 0 0 0 0 0 0 
8| 0 0 0 0 0 0 0 0 0 0 
9| 2 2 2 1 1 1 0 0 0 0 

Whereas we can see that the real solution, based on the output showing us what the lines to write into the grid should be, would look like this:

x  0 1 2 3 4 5 6 7 8 9
----------------------
0| 0 0 0 0 0 0 0 1 0 0
1| 0 0 1 0 0 0 0 1 0 0
2| 0 0 1 0 0 0 0 1 0 0
3| 0 0 0 0 0 0 0 1 0 0
4| 0 1 1 2 1 1 1 2 1 1
5| 0 0 0 0 0 0 0 0 0 0
6| 0 0 0 0 0 0 0 0 0 0
7| 0 0 0 0 0 0 0 0 0 0
8| 0 0 0 0 0 0 0 0 0 0
9| 2 2 2 1 1 1 0 0 0 0

So clearly, even though my code is identifying which lines should be added to the grid, for some reason I can't pinpoint some are not being added. Any thoughts as to what's going wrong here?

CodePudding user response:

Sometimes x2 or y2 is smaller than x1 or y1, and your code isn't accounting for that - your loops are currently doing for (let y = line.y1; y <= line.y2; y ) {, which will not iterate at all if the 2 starts out smaller than the 1.

Either identify the smaller or larger elements first, or identify which direction you need to iterate - with 1 or -1. Eg

const increment = x2 > x1 ? 1 : -1;
for (let x = line.x1; x !== line.x2; x  = increment) {
    grid[line.y1][x]  ;
}
  • Related