Solve a Hidato puzzle: Difference between revisions

Content added Content deleted
m (C# added a remark.)
(add Tailspin solution)
Line 4,030: Line 4,030:
. . . . . . . 5 4 .
. . . . . . . 5 4 .
. . . . . . . . . .
. . . . . . . . . .
</pre>

=={{header|Tailspin}}==
{{trans|Java}}
<lang tailspin>
def input:
'__ 33 35 __ __ . . .
__ __ 24 22 __ . . .
__ __ __ 21 __ __ . .
__ 26 __ 13 40 11 . .
27 __ __ __ 9 __ 1 .
. . __ __ 18 __ __ .
. . . . __ 7 __ __
. . . . . . 5 __';

templates hidato
composer setup
@: {row: 1, col: 1, given:[]};
{ board: [ <line>+ ], given: $@.given -> [i](<{}> { n: $i, $...} !) }
rule line: [ <cell>+ ] (<'\n '>?) (..|@: {row: $@.row + 1, col: 1};)
rule cell: <open|blocked|given> (<' '>?) (@.col: $@.col + 1;)
rule open: <'__'> -> 0
rule blocked: <' \.'> -> -1
rule given: (<' '>?) (def given: <INT>;)
($given -> ..|@.given: $@.given::length+1..$ -> [];)
($given -> @.given($): { row: $@.row, col: $@.col };)
$given
end setup

templates solve
<~{row: <1..$@hidato.board::length>, col: <1..$@hidato.board(1)::length>}> !VOID
<{ n: <$@hidato.given(-1).n>, row: <$@hidato.given(-1).row>, col: <$@hidato.given(-1).col> }> $@hidato.board !
<?($@hidato.board($.row; $.col) <~0|$.n>)> !VOID
<?($@hidato.board($.row; $.col) <0>)?($@hidato.given($.next) <$.n>)> !VOID
<>
def guess: $;
def back: $@hidato.board($.row; $.col);
def next: $ -> (<{n: <$back>}> $.next + 1! <> $.next!);
@hidato.board($.row; $.col): $.n;
0..8 -> { next: $next, n: $guess.n + 1, row: $guess.row + $ / 3 - 1, col: $guess.col + $ mod 3 - 1 } -> #
@hidato.board($.row; $.col): $back;
end solve

@: $ -> setup;
{ next: 1, $@.given(1)... } -> solve !
end hidato

$input -> hidato -> '$... -> '$... -> ' $ -> (<-1> ' .' ! <10..> '$;' ! <> ' $;' !);';
';
' ->!OUT::write
</lang>
{{out}}
<pre>
32 33 35 36 37 . . .
31 34 24 22 38 . . .
30 25 23 21 12 39 . .
29 26 20 13 40 11 . .
27 28 14 19 9 10 1 .
. . 15 16 18 8 2 .
. . . . 17 7 6 3
. . . . . . 5 4
</pre>
</pre>