Are you solving a challenging code problem from the advent of code series? Check out the problem description here.
The task involves processing input data in the form of coordinate lines on a grid (x1,y1 -> x2,y2). The goal is to populate a 2D array with these lines and count the overlapping coordinates. However, there seems to be an issue in my code where only some valid lines are added to the grid despite all being identified correctly. Here's a snippet of what I have so far:
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;
}
}
// Rest of the code remains unchanged
}
// Main function call remains same
main();
The output generated by this code shows that not all expected lines are being added to the grid. The actual solution should look different based on the correct lines to be added. Can you spot why certain lines are not being included as they should?