Last active
May 4, 2024 04:27
-
-
Save righ1113/de4ee808da023f2c51088044552a05bf to your computer and use it in GitHub Desktop.
川渡り問題 in Egison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- River | |
-- | |
-- $ egison 4.1.3 | |
-- > loadFile "river.egi" | |
-- | |
-- Data | |
-- | |
-- (旅人, オオカミ, ヒツジ, キャベツ), 0:left 1:right | |
def state := | |
[(0, 0, 0, 0), (0, 1, 1, 1), (0, 0, 1, 1), (0, 0, 0, 1), (1, 1, 1, 1), (1, 1, 1, 2)] | |
-- | |
-- Codes 未完成 | |
-- | |
def isTrans (x1, x2, x3, x4) (y1, y2, y3, y4) := | |
let z := x1 + x2 + x3 + x4 - (y1 + y2 + y3 + y4) | |
in z = -1 || z = 0 || z = 1 | |
-- 各状態に高々一回遷移する事を想定 | |
def river := | |
matchAll state as multiset (integer, integer, integer, integer) with | |
| ((#0, #0, #0, #0) & $x_1) | |
:: (loop $i (2, $n) (($x_i & ?(\_->isTrans x_(i - 1) x_i)) :: ...) | |
(((#1, #1, #1, #1) & ?(\_->isTrans x_n (1, 1, 1, 1))) :: _)) | |
-> (map (\i -> x_i) [1..n]) ++ [(1, 1, 1, 1)] | |
-- | |
-- Tests | |
-- | |
-- $ egison -t river.egi | |
assertEqual "test1" | |
river | |
[[(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 1), (0, 1, 1, 1), (1, 1, 1, 1)]] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment