4 个版本

0.2.2 2022 年 10 月 19 日
0.2.1 2022 年 10 月 6 日
0.2.0 2022 年 10 月 6 日
0.1.0 2022 年 10 月 6 日

#435 in 游戏

MIT 许可证

19KB
330

Rust Sudoku Solver

我用 C 语言编写的数独求解器,但为了练习 Rust 语言而重新编写。

这个版本更健壮,具有更好的 CLI 参数解析器和新功能。

v0.2 更新

在学习 Rust 几个月后,我更新了程序,使其更符合 Rust 语言的习惯并更健壮。我添加了 bpafanyhow 以创建更好的 CLI。我还添加了添加输入和输出文件的功能。它们分别回退到 stdin 和 stdout(允许数据通过管道输入或重定向到程序。我还添加了多种打印类型,包括 simplemultilinebordered(以前的 方法)。simple 将所有内容打印到同一行上,multiline 将所有内容仍然打印成网格,但没有围绕每个方框的边框,而 bordered 则打印围绕谜题和每个 3x3 区域的边框。

安装

  • cargo
$ cargo install rust-sudoku
  • 预构建的二进制文件

从版本中获取预构建的二进制文件。

用法

$ rust-sudoku --help
Solve Sudoku problems *blazingly fast*.
Accepts input from a text file to fill in the puzzle. Each number (1-9) is interpreted
as that number in the puzzle. A zero or any other letter is considered a blank space.
Any whitespace is ignored.

Usage: [-i FILE] [-o FILE] [--style STYLE] [-p] [-d DELAY]

Available options:
    -i, --input <FILE>    Location of puzzle to read or stdin by default.
    -o, --output <FILE>   Output file to write solution to or stdout by default.
        --style <STYLE>   Print puzzle with nice borders, options include `simple`, `multiline` and `bordered`.
    -p, --print-partials  Print each partial solution to the console as the program runs.
    -d, --delay <DELAY>   Add delay between each iteration in ms (useful when using `--print-partials`).
    -h, --help            Prints help information
    -V, --version         Prints version information

示例文件

sudoku puzzle in txt format

转换为(作为图像)

sudoku puzzle converted

演示

YouTube 上的完整视频

Rust_Sudoku_Solver

原始帖子(类似于博客)

变更

除了这个项目是用新语言重写的之外,最大的不同之处在于我实现了树的方式。因为 Rust 需要额外的安全性,并不太赞成我在 C 中使用的自引用结构,所以我必须做出一些更改。而不是在堆上自由存储节点,它们存储在 Vec 中。每个新的节点都推送到 Vec 中。因此,在每个节点结构中,而不是持有指向其子节点的指针,它存储在 Vec 中的子节点的索引。许多其他东西都保持与我的 C 实现类似,如果不是相同的方式。

Rust

用Rust进行第一个项目实际上非常有趣。从在C/C++中构建一些项目来说,编译器的出色性能让我感到非常震惊。它为你提供关于每一件事的建议。与手动创建Makefiles相比,我也很享受使用cargo的便捷性。我还玩了一段时间的rustfmt。所有这些工具都让使用Rust变得非常有趣。更重要的是,我根本不需要担心内存泄漏、悬挂指针或野指针。所有的堆分配都由系统处理。我不得不说,使用Rust真的非常有趣。

Ferris the Crab

依赖项

~2MB
~45K SLoC