Skip to main content

アカリ(美術館 / Light Up)パズルを SAT ソルバーで解くライブラリ

Project description

akari-sat

アカリ(美術館 / Light Up)パズルを SAT ソルバーで解く Python ライブラリです。 盤面のルールを CNF 制約に変換し、PySAT(Glucose3)で求解します。

インストール

pip install akari-sat

uv を使う場合:

uv add akari-sat

使い方

インストール後、akari_sat パッケージから solve_akari を読み込んで使います。

from akari_sat import solve_akari

grid = [
    "..#..",
    "2....",
    "..1..",
    "....0",
    "..#..",
]
H, W = len(grid), len(grid[0])

result = solve_akari(H, W, grid)  # 解が存在しない場合は None

if result is None:
    print("解が見つかりませんでした(充足不能)")
else:
    for r in range(H):
        print("".join("*" if result[r][c] else grid[r][c] for c in range(W)))

実行結果(* が灯り):

*.#.*
2..*.
.*1..
...*0
.*#*.

API

solve_akari(H, W, grid, solver=Glucose3)

アカリを解きます。

引数:

  • H (int): グリッドの高さ。
  • W (int): グリッドの幅。
  • grid (list[str] | list[list[str]]): 盤面。1 行 1 文字列のリスト、または 1 文字ずつの 2 次元配列で渡します。
  • solver: 使用する PySAT のソルバークラス。デフォルトは Glucose3

戻り値:

  • list[list[bool]]: H×W の真偽値の 2 次元配列。result[r][c] が r 行 c 列に灯があるかを表します。
  • 解が存在しない場合は None

各マスの文字の意味:

文字 意味
. 空きマス
# 数字のない壁
04 数字付きの壁

エンコードしているルール

アカリの 4 つの規則を制約として SAT に与えています。

  • 壁・数字マスには灯を置かない。
  • 数字マスの上下左右に接する灯の数は、その数字とちょうど一致する。
  • 同じ行・列で壁を挟まずに 2 つ以上の灯を置かない(灯どうしは照らし合わない)。
  • すべての空きマスがいずれかの灯に照らされる。

動作環境

  • Python 3.13 以上
  • python-sat(依存パッケージとして自動的に導入されます)

リポジトリ

https://github.com/youseiushida/akari-sat

ライセンス

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

akari_sat-0.1.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

akari_sat-0.1.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file akari_sat-0.1.1.tar.gz.

File metadata

  • Download URL: akari_sat-0.1.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.12

File hashes

Hashes for akari_sat-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1b34e3537ad5e294f5b52cb0caff26b3b3fb8d74f63665081377aeed387d118c
MD5 b25ab2719830cc05ee50c47f2b89d9c6
BLAKE2b-256 8460c01b0392dfcd9e75b635ea0e3ac10ce44dfb317cab89148f6df7b79633f3

See more details on using hashes here.

File details

Details for the file akari_sat-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: akari_sat-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.12

File hashes

Hashes for akari_sat-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 deff62bf14347f64909c3d164854e7b807a61bab7228502002ea5cf3c7bc4dc5
MD5 a593111aa4aa27461f48d54c55905a37
BLAKE2b-256 a2f5b3838fb9dd77f3b3aa4aaa9876b25faa709a6eaed8f51f3cfba57ed13bd9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page