Solve hanging lantern problem: Difference between revisions
m
→{{header|J}}
(J entry) |
m (→{{header|J}}) |
||
Line 427:
Examples of this approach are left as an exercise for the user.
Finally, enumerating solutions might be approached recursively:
<lang J>showlanterns=: {{
arrange=. ($ $ (* +/\)@,) y $&>1
echo 'lantern ids:'
echo rplc&(' 0';' ')"1 ' ',.":|:arrange
echo ''
cols=. <@-.&0"1 arrange
recur=: <@{{
todo=. (#~ ~:&a:) y -.L:0 x
if. #todo do.
next=. {:@> todo
,x <@,S:0 every next recur todo
else.
<x
end.
}}"0 1
echo 'all lantern removal sequences:'
echo >a:-.~ -.&0 each;0 recur cols
}}</lang>
Example use:
<lang J> showlanterns 1 2 1
lantern ids:
1 2 4
3
all lantern removal sequences:
1 3 2 4
1 3 4 2
1 4 3 2
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 3 2
4 3 1 2
4 3 2 1</lang>
=={{header|Julia}}==
|