【问题标题】:Writing coordinate lines onto a 2D array representing a grid将坐标线写入表示网格的二维数组
【发布时间】:2022-01-16 01:42:52
【问题描述】:

我正在尝试解决代码出现后的以下代码挑战:problem description

基本上,我接收的输入形式为表示网格上坐标线的线,即:x1,y1 -> x2,y2。对于问题的第一部分,我要将这些线绘制成一个二维数组,然后计算网格中线至少重叠一次的坐标数。到目前为止,我已经成功地隔离了哪些线是有效的(水平或垂直),并试图简单地将它们绘制到二维数组上。由于一些我无法确定的奇怪原因,我的方法只是在数组上绘制了一些线,即使这些线都被标识为有效的应该并且被传递到应该绘制的相同代码块中行到数组中。见以下代码:

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();

此代码将输出以下内容:

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 

而我们可以看到,真正的解决方案,根据输出显示我们要写入网格的行应该是这样的:

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

很清楚,即使我的代码正在确定应该将哪些行添加到网格中,但由于某种原因,我无法确定有些行没有被添加。关于这里出了什么问题的任何想法?

【问题讨论】:

    标签: arrays typescript algorithm


    【解决方案1】:

    有时 x2 或 y2 小于 x1 或 y1,您的代码没有考虑到这一点 - 您的循环当前正在执行 for (let y = line.y1; y &lt;= line.y2; y++) {,如果 2 开始小于 1,则根本不会迭代。

    要么首先确定较小或较大的元素,要么确定您需要迭代的方向 - 使用 +1 或 -1。例如

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

    【讨论】:

    • 啊,我犯了一个多么愚蠢的错误,我应该更仔细地分析输入数据,而不是仔细考虑我的代码。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    相关资源
    最近更新 更多