【发布时间】: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