代码来临 2024 年第 4 天
第 1 部分
x 标记(数百个?)点令我惊讶的是,到目前为止还没有像这样的文字搜索谜题。
这似乎令人畏惧,但我的策略是:
1
2
3
4
5
find the index of each x in the grid
for each x
check the next three letters in a straight path in each of the eight directions
if the path ends up spelling xmas
add one to a running total
在示例中检查此策略使我相信这是一种成功的方法。
现在是令人兴奋的部分:从头开始编码整个事情!
找到网格中每个 x 的索引...最终首先,我必须将输入解析为二维字符数组:
1
2
let grid = input.split(
).map(line => line.split())
我在网格谜题中经常遇到的一个障碍是考虑越界索引。
如果我从边界单元格 - 或靠近边界的单元格开始 - 并朝边缘方向走这么远,我最终会遇到越界的行或列。
我有两种策略来处理这个问题:
向我的条件添加检查以查找不存在的行或列 用足够的行和列填充网格,这样就不会出现越界的风险对于这个挑战,我选择#2。
用 3 单元格厚边框填充网格,如下所示:
1
2
3
4
5
6
7
8
9
10
grid = grid.map(line => [.,.,.,...line,.,.,.])
grid = [
new array(grid[0].length).fill(.),
new array(grid[0].length).fill(.),
new array(grid[0].length).fill(.),
...grid,
new array(grid[0].length).fill(.),
new array(grid[0].length).fill(.),
new array(grid[0].length).fill(.)
]
示例网格现在如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
................
................
................
...mmmsxxmasm...
...msamxmsmsa...
...amxsxmaamm...
...msamasmsmx...
...xmasamxamm...
...xxammxxama...
...smsmsasxss...
...saxamasaaa...
...mammmxmmmm...
...mxmxaxmasx...
................
................
................
1
2
3
4
5
6
7
8
let xs = []
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid[0].length; col++) {
if (grid[row][col] == "x") {
xs.push([row, col])
}
}
}
成功:在示例网格中找到了所有 19 个 x!
从每个x向八个方向走三步所有八个相对坐标都编码为 8 元素数组:
1
2
3
4
5
6
7
8
9
10
let dirs = [
[-1,-1],
[-1,0],
[-1,1],
[0,-1],
[0,1],
[1,-1],
[1,0],
[1,1]
]
现在主要算法:
1
2
3
4
5
6
7
8
for each x
for each direction
create an array that starts with x
do 3 times
move one cell in this direction
add the value of that cell to the array
check whether the concatenation of all four values is "xmas"
if it is, increment a tally
在 javascript 中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
xs.reduce((total, coord) => {
dirs.foreach((dir) => {
let [row, col] = coord;
let [y, x] = dir;
let word = ["x"];
for (let i = 0; i < 3; i++) {
row += y;
col += x;
word.push(grid[row][col]);
}
if (word.join("") == "xmas") {
total++;
}
});
return total;
}, 0);
它为示例输入生成正确的答案!
当我在拼图输入上运行它时会发生什么?!!
我得到一个数字:几千个“xmas”
这是正确答案吗?
就是这样!!!
呜呼!!!
迫不及待地想看看第二部分有什么精彩内容......
第2部分
哦天哪。这变得有点复杂了。但可行!在第 1 部分中,我一直在寻找 x。
现在,我正在寻找
女士在第 1 部分中,我将字母写成一条直线来组成单词。
现在,我正在寻找 5 单元短语的四种配置:
1
2
3
m s m m s m s s
a a a a
m s s s s m m m
一个 m 可以是多个 x-mas 的一部分。
通过检查每个m,我很可能会遇到好几次。
我需要为每场比赛构建一个字符串化坐标的 set()。这样,我只占一个 x-mas 实例一次。
突然——辉煌! - 主意我不会检查每一个m。
我会检查每一个a。
我将按顺时针顺序检查对角相邻的四个单元格。
x-mas 比赛将符合以下四种模式之一:
1
2
3
4
MSSM
MMSS
SMMS
SSMM
`
唷!这比我最初的想法要简单得多。
而且我应该能够重新利用我的大部分第 1 部分代码!
复制粘贴调整在网格中找到所有 as:
js
让 as = [];
for (let row = 0; row for (let col = 0; col if (网格[行][列] == "a") {
as.push([行, 列]);
}
}
}
建立要检查的相对坐标顺序:
js
让阿迪尔斯 = [
[-1, -1],
[-1, 1],
[1, 1],
[1, -1],
];
将比赛的得分相加:
js
让part2 = as.reduce((总计, 坐标) => {
让顺时针 = adirs.map((dir) => {
让 [行,列] = 坐标;
让 [y, x] = dir;
返回网格[行 y][列 x];
});
if (["mssm", "mmss", "smms", "ssmm"].includes(顺时针.join(""))) {
总计 ;
}
返回总计;
}, 0);
它为示例输入生成正确的答案!
现在检查我的拼图输入...
确实!!!正确答案!!!
我很高兴我突然想到使用 as 而不是 ms.
我确信,为我节省了数小时的时间来解决头痛问题。
这是另一个有趣且容易上手的谜题!
我想知道第五天会发生什么。
以上就是谷神星搜索的详细内容,更多请关注php中文网其它相关文章!