Sudoku: Difference between revisions

Content added Content deleted
(→‎{{header|Tailspin}}: Type bounds + link to blog post)
Line 13,071: Line 13,071:


=={{header|Tailspin}}==
=={{header|Tailspin}}==
There is a blog post about how this code was developed: https://tobega.blogspot.com/2020/05/creating-algorithm.html
<syntaxhighlight lang="tailspin">
<syntaxhighlight lang="tailspin">
templates deduceRemainingDigits
templates deduceRemainingDigits
templates findOpenPosition
templates findOpenPosition
@:{options: 10"1"};
@:{options: 10"1"};
$ -> \[i;j](when <[](..~$@findOpenPosition.options::raw)> do @findOpenPosition: {row: $i, col: $j, options: ($::length)"1"}; \) -> !VOID
$ -> \[i;j](when <[]?($::length <..~$@findOpenPosition.options::raw>)> do @findOpenPosition: {row: $i, col: $j, options: ($::length)"1"}; \) -> !VOID
$@ !
$@ !
end findOpenPosition
end findOpenPosition
Line 13,092: Line 13,093:
@: $;
@: $;
$ -> findOpenPosition -> #
$ -> findOpenPosition -> #
when <{options: <=0"1">}> do [] !
when <{options: <=0"1">}> do row´1:[] !
when <{options: <=10"1">}> do $@ !
when <{options: <=10"1">}> do $@ !
when <> do def next: $;
when <> do def next: $;
$@ -> selectFirst&{pos: $next} -> deduceRemainingDigits
$@ -> selectFirst&{pos: $next} -> deduceRemainingDigits
-> \(when <~=[]> do @deduceRemainingDigits: $; {options: 10"1"} !
-> \(when <~=row´1:[]> do @deduceRemainingDigits: $; {options: 10"1"} !
when <=[]> do ^@deduceRemainingDigits($next.row;$next.col;1)
when <=row´1:[]> do ^@deduceRemainingDigits($next.row;$next.col;1)
-> { $next..., options: $next.options-1"1"} ! \) -> #
-> { $next..., options: $next.options-1"1"} ! \) -> #
end deduceRemainingDigits
end deduceRemainingDigits
Line 13,122: Line 13,123:
assert row´1:[
assert row´1:[
col´1:[[],3,4,6,7,8,9,1,2],
col´1:[[],3,4,6,7,8,9,1,2],
$sample(row´2..last)...] -> deduceRemainingDigits <=[]> 'no remaining options returns empty'
$sample(row´2..last)...] -> deduceRemainingDigits <=row´1:[]> 'no remaining options returns empty'


assert row´1:[
assert row´1:[
Line 13,183: Line 13,184:
templates solveSudoku
templates solveSudoku
$ -> parseSudoku -> deduceRemainingDigits -> #
$ -> parseSudoku -> deduceRemainingDigits -> #
when <=[]> do 'No result found' !
when <=row´1:[]> do 'No result found' !
when <> do $ -> \[i](
when <> do $ -> \[i](
'$(col´1..col´3)...;|$(col´4..col´6)...;|$(col´7..col´9)...;$#10;' !
'$(col´1..col´3)...;|$(col´4..col´6)...;|$(col´7..col´9)...;$#10;' !