Towers of Hanoi: Difference between revisions
Content added Content deleted
(→{{header|Groovy}}: new solution) |
|||
Line 696: | Line 696: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|Groovy}}== |
|||
Unlike most solutions here this solution manipulates more-or-less actual stacks of more-or-less actual rings. |
|||
<lang groovy>def tail = { list, n -> def m = list.size(); list[([m - n, 0].max())..<m] } |
|||
final STACK = [A:[],B:[],C:[]].asImmutable() |
|||
def report = { it -> } |
|||
def check = { it -> } |
|||
def moveRing = { from, to -> to << from.pop(); report(); check(to) } |
|||
def moveStack |
|||
moveStack = { from, to, using = STACK.values().find { !(it.is(from) || it.is(to)) } -> |
|||
if (!from) return |
|||
def n = from.size() |
|||
moveStack(tail(from, n-1), using, to) |
|||
moveRing(from, to) |
|||
moveStack(tail(using, n-1), to, from) |
|||
}</lang> |
|||
Test program: |
|||
<lang groovy>enum Ring { |
|||
S('°'), M('o'), L('O'), XL('()'); |
|||
private sym |
|||
private Ring(sym) { this.sym=sym } |
|||
String toString() { sym } |
|||
} |
|||
report = { STACK.keySet().each { println "${it}: ${STACK[it]}" }; println() } |
|||
check = { to -> assert to == ([] + to).sort().reverse() } |
|||
Ring.values().reverseEach { STACK.A << it } |
|||
report() |
|||
check(STACK.A) |
|||
moveStack(STACK.A, STACK.C)</lang> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |