Code Golf:验证数独网格
简介
有效的数独网格由数字 1 到 9 填充,并且数字在 9、行或列的每个子块中出现的次数不会超过一次。如果您不熟悉这个流行的谜题,请阅读这篇文章了解更多详细信息。
挑战
挑战是编写最短的程序来验证可能未满的数独网格。
输入将是一个包含 9 行、每行 9 个字符的字符串,代表网格。空单元格将由 .
表示。如果网格有效,则输出应为 Valid
,否则输出 Invalid
。
示例
输入
123...789
...456...
456...123
789...456
...123...
564...897
...231...
897...564
...564...
输出
Valid
输入
123456789
987654321
123456789
123456789
987654321
123456789
123456789
987654321
123456789
输出
Invalid
代码 高尔夫规则
请发布解决此问题的任何语言的最短代码。输入和输出可以通过 stdin 和 stdout 或您选择的其他文件进行处理。
获胜者将是在发布此问题之前已存在实现的语言中最短的解决方案(按字节数计算)。因此,虽然您可以自由地使用刚刚编写的语言来提交 0 字节解决方案,但它不算数,而且您可能会得到否决票。
Introduction
A valid Sudoku grid is filled with numbers 1 to 9, with no number occurring more than once in each sub-block of 9, row or column. Read this article for further details if you're unfamiliar with this popular puzzle.
Challenge
The challenge is to write the shortest program that validates a Sudoku grid that might not be full.
Input will be a string of 9 lines of 9 characters each, representing the grid. An empty cell will be represented by a .
. Your output should be Valid
if the grid is valid, otherwise output Invalid
.
Example
Input
123...789
...456...
456...123
789...456
...123...
564...897
...231...
897...564
...564...
Output
Valid
Input
123456789
987654321
123456789
123456789
987654321
123456789
123456789
987654321
123456789
Output
Invalid
Code Golf Rules
Please post your shortest code in any language that solves this problem. Input and output may be handled via stdin and stdout or by other files of your choice.
Winner will be the shortest solution (by byte count) in a language with an implementation existing prior to the posting of this question. So while you are free to use a language you've just made up in order to submit a 0-byte solution, it won't count, and you'll probably get downvotes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
高尔夫球脚本:56
Golfscript: 56
C:
165162161160159不需要两个换行符。 josefx 保存了一个字符:-) ...
C:
165162161160159The two newlines are not needed. One char saved by josefx :-) ...
哈斯克尔:
207230218195172Haskell:
207230218195172Perl:
168128第一个正则表达式检查同一行和同一列中的重复项;第二个正则表达式处理“同一个框”中的重复项。
通过将第一个正则表达式中的
\n
替换为文字换行符(1 个字符),或者使用 >= Perl 5.12,替换[^\n]
,可以进一步改进与\N
(3 char)早些时候,168 字符解决方案:
输入来自标准输入,输出到stderr,因为它使事情变得如此简单。换行符是可选的,不计算在内。
Perl:
168128The first regex checks for duplicates that are in the same row and column; the second regex handles duplicates in the "same box".
Further improvement is possible by replacing the
\n
in the first regex with a literal newline (1 char), or with >= Perl 5.12, replacing[^\n]
with\N
(3 char)Earlier, 168 char solution:
Input is from stdin, output is to stderr because it makes things so easy. Linebreaks are optional and not counted.
Python:
230221200185首先是 len=199 处的可读版本:
由于 SO 不显示制表符,因此我使用了 < code>表示单个制表符。
附言。同样的方法将 minEvilized 减少到 185 个字符:
Python:
230221200185First the readable version at len=199:
Since SO doesn't display tab characters, I've used
<T>
to represent a single tab character.PS. the same approach minEvilized down to 185 chars:
Perl,153 个字符
@B
包含棋盘的 81 个元素。&E
测试@B
的子集是否包含任何重复数字主循环验证拼图的每一列、“块”和行
Perl, 153 char
@B
contains the 81 elements of the board.&E
tests whether a subset of@B
contains any duplicate digitsmain loop validates each column, "block", and row of the puzzle
Python:
159158Python:
159158<T> is a single tab character
Common Lisp:
266252Common Lisp:
266252Perl:186
输入来自 stdin,输出到 stdout,输入中的换行符可选。
(为了“清晰”而添加换行符。)
c()
是一个函数,用于根据作为参数传递的位置编号列表来检查@y
中的输入。如果所有位置列表都有效(包含的数字不超过一次),则返回 0,否则返回 1,使用递归检查每个列表。最后一行构建此列表列表,将其传递给c()
并使用结果选择要输出的正确前缀。我非常喜欢的一件事是,这个解决方案利用了
@b
中“块”位置列表中的“自相似性”(它被冗余地重建了很多次以避免出现@b =...
在单独的语句中):整个拼图中第 i 个块的左上角位置可以通过将@b
中的第 i 个元素乘以 3 找到。更多传播出去:
Perl: 186
Input is from stdin, output to stdout, linebreaks in input optional.
(Linebreaks added for "clarity".)
c()
is a function that checks the input in@y
against a list of lists of position numbers passed as an argument. It returns 0 if all position lists are valid (contain no number more than once) and 1 otherwise, using recursion to check each list. The bottom line builds this list of lists, passes it toc()
and uses the result to select the right prefix to output.One thing that I quite like is that this solution takes advantage of "self-similarity" in the "block" position list in
@b
(which is redundantly rebuilt many times to avoid having@b=...
in a separate statement): the top-left position of the ith block within the entire puzzle can be found by multiplying the ith element in@b
by 3.More spread out:
Perl: 202
我正在阅读 Modern Perl,感觉想编写一些东西...(顺便说一句,这是一本很酷的书:)
Count 排除了不必要的换行符。
这可能需要 Perl 5.12.2。
更具可读性:
Perl: 202
I'm reading Modern Perl and felt like coding something... (quite a cool book by the way:)
Count is excluding unnecessary newlines.
This may require Perl 5.12.2.
A bit more readable:
红宝石 — 176
Ruby — 176
Lua,341 字节
虽然我知道 Lua 不是最好的高尔夫语言,但是,考虑到它的大小,我认为值得发布它;)。
非高尔夫版本,带注释和错误打印版本,以获得更多乐趣:)
高尔夫版本,341 字节
Lua, 341 bytes
Although I know that Lua isn't the best golfing language, however, considering it's size, I think it's worth posting it ;).
Non-golfed, commented and error-printing version, for extra fun :)
Golfed version, 341 bytes
蟒蛇:140
Python: 140
ASL:108
ASL 是我制作的受 Golfscript 启发的脚本语言。
ASL: 108
ASL is a Golfscript inspired scripting language I made.