将二维数组复制到第二个更大数组的中间
Copy 2D array into a middle of second bigger array
您好,我正在努力将一个数组复制到其他更大数组的中间。即使指标正确,我也看不出为什么它不能工作的原因。这是 https://adventofcode.com/2020/day/17。他们要求 3D 数组,但我需要先用 2D 数组解决它。
我有一个像这样的空数组
[
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ]
]
和小数组
[
[ 'X', '#' ],
[ '#', 'X' ]
]
我的目标是像这样把小数组放在大空数组的中间
[
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', 'X','#', '', ''],
[ '', '', '#','X', '', ''],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ]
]
但我明白了
[
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ]
]
我的密码是
let square = [ [ 'X', '#' ], [ '#', 'X' ] ]
let cycles = 2;
let startWidth = square.length;//square is the small array
let finalWidth = startWidth + (cycles * 2);//cycles is part of the task it tells
let twoD = new Array(finalWidth).fill(new Array(finalWidth).fill('')); how big is the bigger array/how many spaces from the edge to the small array
if(finalWidth % 2 == 0){//just made for even numbers once I make this work will edit it for odd too
for(let row = (finalWidth / 2) - 1; row < (finalWidth / 2) + startWidth - 1; row++){
for(let col = (finalWidth / 2) - 1; col < (finalWidth / 2) + startWidth - 1; col++){
let i = col - (finalWidth / 2) + 1;
let j = row - (finalWidth / 2) + 1;
console.log(i, j, row, col)//logs correct indecies
twoD[row][col] = square[i][j]
}
}
}
如有任何想法,我们将不胜感激。谢谢!
你可以试试这个逻辑:
- 创建 2 个变量:
midRow
: 保持x位置
midCol
: 保持y位置
- 然后遍历提供的值数组。
- 使用
midRow
和midCol
计算写入位置。
- 覆盖值并打印它
const matrix = [
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ]
]
const value = [
[ 'X', '#' ],
[ '#', 'X' ]
]
const midRow = Math.floor((matrix.length - 1)/2)
const midCol = Math.floor((matrix[0].length - 1)/2)
for (let r = 0; r < value.length; r++) {
for (let c = 0; c < value[0].length; c++) {
matrix[r + midRow][c + midCol] = value[r][c]
}
}
console.log(matrix.join('\n'))
我还看到您正在使用它来创建数组:
let matrix = new Array(6).fill(new Array(6).fill(''));
Array.fill
的问题是它首先创造价值,然后将相同的价值推向所有项目。通常它不会引起问题,但由于您正在填充 Array
,因此您正在复制 1 个数组的 6 个位置的引用。因此改变一个将影响所有
因此你的问题:
let matrix = new Array(6).fill(new Array(6).fill(''));
matrix[2][2] = '#'
console.log(matrix.join('\n'))
解决方案:
使用Array.from()
。这将创建动态数组,您可以根据需要填充
let matrix = Array.from(
{ length: 6},
() => Array.from({length: 6}, () => '')
)
matrix[2][2] = '#'
console.log(matrix.join('\n'))
您好,我正在努力将一个数组复制到其他更大数组的中间。即使指标正确,我也看不出为什么它不能工作的原因。这是 https://adventofcode.com/2020/day/17。他们要求 3D 数组,但我需要先用 2D 数组解决它。
我有一个像这样的空数组
[
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ]
]
和小数组
[
[ 'X', '#' ],
[ '#', 'X' ]
]
我的目标是像这样把小数组放在大空数组的中间
[
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', 'X','#', '', ''],
[ '', '', '#','X', '', ''],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ]
]
但我明白了
[
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ],
[ '', '', '#', 'X', '', '' ]
]
我的密码是
let square = [ [ 'X', '#' ], [ '#', 'X' ] ]
let cycles = 2;
let startWidth = square.length;//square is the small array
let finalWidth = startWidth + (cycles * 2);//cycles is part of the task it tells
let twoD = new Array(finalWidth).fill(new Array(finalWidth).fill('')); how big is the bigger array/how many spaces from the edge to the small array
if(finalWidth % 2 == 0){//just made for even numbers once I make this work will edit it for odd too
for(let row = (finalWidth / 2) - 1; row < (finalWidth / 2) + startWidth - 1; row++){
for(let col = (finalWidth / 2) - 1; col < (finalWidth / 2) + startWidth - 1; col++){
let i = col - (finalWidth / 2) + 1;
let j = row - (finalWidth / 2) + 1;
console.log(i, j, row, col)//logs correct indecies
twoD[row][col] = square[i][j]
}
}
}
如有任何想法,我们将不胜感激。谢谢!
你可以试试这个逻辑:
- 创建 2 个变量:
midRow
: 保持x位置midCol
: 保持y位置
- 然后遍历提供的值数组。
- 使用
midRow
和midCol
计算写入位置。 - 覆盖值并打印它
const matrix = [
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ],
[ '', '', '', '', '', '' ]
]
const value = [
[ 'X', '#' ],
[ '#', 'X' ]
]
const midRow = Math.floor((matrix.length - 1)/2)
const midCol = Math.floor((matrix[0].length - 1)/2)
for (let r = 0; r < value.length; r++) {
for (let c = 0; c < value[0].length; c++) {
matrix[r + midRow][c + midCol] = value[r][c]
}
}
console.log(matrix.join('\n'))
我还看到您正在使用它来创建数组:
let matrix = new Array(6).fill(new Array(6).fill(''));
Array.fill
的问题是它首先创造价值,然后将相同的价值推向所有项目。通常它不会引起问题,但由于您正在填充 Array
,因此您正在复制 1 个数组的 6 个位置的引用。因此改变一个将影响所有
因此你的问题:
let matrix = new Array(6).fill(new Array(6).fill(''));
matrix[2][2] = '#'
console.log(matrix.join('\n'))
解决方案:
使用Array.from()
。这将创建动态数组,您可以根据需要填充
let matrix = Array.from(
{ length: 6},
() => Array.from({length: 6}, () => '')
)
matrix[2][2] = '#'
console.log(matrix.join('\n'))