Solve a Hidato puzzle: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(→{{header|Tailspin}}: added required typing) |
||
Line 4,770: | Line 4,770: | ||
. . . . __ 7 __ __ |
. . . . __ 7 __ __ |
||
. . . . . . 5 __'; |
. . . . . . 5 __'; |
||
templates hidato |
templates hidato |
||
composer setup |
composer setup |
||
data givenInput <[<={}|{row: <row>, col: <col>}>*]> local |
data givenInput <n´1:[<´{}´ ={}|{row: <row>, col: <col>}>*]> local |
||
@: {row: 1, col: 1, givenInput:[]}; |
@: {row: 1, col: 1, givenInput:n´1:[]}; |
||
{ board: [ <line>+ ], given: $@.givenInput -> \[i](<~={}> { n: $i, $...} !\) } |
{ board: row´1:[ <line>+ ], given: $@.givenInput -> \[i](<~´{}´ ={}> { n: $i, $...} !\) } |
||
rule line: [ <cell>+ ] (<'\n '>?) (..|@: {row: $@.row::raw + 1, col: 1};) |
rule line: col´1:[ <cell>+ ] (<'\n '>?) (..|@: {row: $@.row::raw + 1, col: 1};) |
||
rule cell: <open|blocked|given> (<' '>?) (@.col: $@.col::raw + 1;) |
rule cell: <open|blocked|given> (<' '>?) (@.col: $@.col::raw + 1;) |
||
rule open: <'__'> -> 0 |
rule open: <'__'> -> n´0 |
||
rule blocked: <' \.'> -> -1 |
rule blocked: <' \.'> -> n´-1 |
||
rule given: (<' '>?) (def given: <INT>;) |
rule given: (<' '>?) (def given: <n´INT>;) |
||
($given -> ..|@.givenInput: $@.givenInput::length+1..$ -> {};) |
($given -> ..|@.givenInput: $@.givenInput::length+1..$::raw -> {};) |
||
($given -> @.givenInput($): { row: $@.row, col: $@.col };) |
($given -> @.givenInput($): { row: $@.row, col: $@.col };) |
||
$given |
$given |
||
end setup |
end setup |
||
templates solve |
templates solve |
||
when <~{row: <1..$@hidato.board::length>, col: <1..$@hidato.board(1)::length>}> do !VOID |
when <~{row: <1..$@hidato.board::length>, col: <1..$@hidato.board(row´1)::length>}> do !VOID |
||
when <{ n: <=$@hidato.given(last).n>, row: <=$@hidato.given(last).row>, col: <=$@hidato.given(last).col> }> do $@hidato.board ! |
when <{ n: <=$@hidato.given(last).n>, row: <=$@hidato.given(last).row>, col: <=$@hidato.given(last).col> }> do $@hidato.board ! |
||
when <?($@hidato.board($.row; $.col) <~=0|=$.n |
when <?($@hidato.board($.row; $.col) <~=n´0|=$.n>)> do !VOID |
||
when <?($@hidato.board($.row; $.col) <=0>)?($@hidato.given($.next) <=$.n |
when <?($@hidato.board($.row; $.col) <=n´0>)?($@hidato.given($.next) <{n: <=$.n>}>)> do !VOID |
||
otherwise |
otherwise |
||
def guess: $; |
def guess: $; |
||
def back: $@hidato.board($.row; $.col); |
def back: $@hidato.board($.row; $.col); |
||
def next: $ -> \(when <{n: <=$back>}> do $.next::raw + 1! otherwise $.next!\); |
def next: $ -> \(when <{n: <=$back>}> do n´($.next::raw + 1)! otherwise $.next!\); |
||
@hidato.board($.row; $.col): $.n |
@hidato.board($.row; $.col): $.n; |
||
0..8 -> { next: $next, n: $guess.n::raw + 1, row: $guess.row::raw + $ ~/ 3 - 1, col: $guess.col::raw + $ mod 3 - 1 } -> # |
0..8 -> { next: $next, n: $guess.n::raw + 1, row: $guess.row::raw + $ ~/ 3 - 1, col: $guess.col::raw + $ mod 3 - 1 } -> # |
||
@hidato.board($.row; $.col): $back; |
@hidato.board($.row; $.col): $back; |
||
end solve |
end solve |
||
@: $ -> setup; |
@: $ -> setup; |
||
{ next: 1, $@.given( |
{ next: n´1, $@.given(first)... } -> solve ! |
||
end hidato |
end hidato |
||
$input -> hidato -> '$... -> '$... -> ' $ -> \(when <=-1> do ' .' ! when <10..> do '$;' ! otherwise ' $;' !\);'; |
$input -> hidato -> '$... -> '$... -> ' $ -> \(when <=n´-1> do ' .' ! when <n´10..> do '$;' ! otherwise ' $;' !\);'; |
||
'; |
'; |
||
' ->!OUT::write |
' ->!OUT::write |