Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a starter for 10, in Scala, revised a few times. I don't know of any issues, and I have no other ideas for further reducing the moves</p> <p>Runs as a Scala script.</p> <p>Bits of this are quite elegant (IMO) but other bits are an ugly hack</p> <p>Shortest code (but non-optimal moves), tracking position of disks rather than list of disks on rods (idea shamelessly stolen from the Perl solution)</p> <pre><code> val r=args(0).split(",",-1);var d=Map{{for(q&lt;-0 to 2 if""!=r(q);n&lt;-r(q).split('-').map{_.toInt})yield(n,q+1)}:_*};val n=d.max._1;var m="";def s(f:Int,t:Int,n:Int):Unit=if(n!=0&amp;&amp;f!=t){s(f,6-f-t,n-1);d=d+(n-&gt;t);m=m+","+f+t;s(6-f-t,t,n-1)};for(c&lt;- 2 to n)s(d(1),d(c),c-1);if(m=="")s(d(1),d(1)%3+1,n);println(m.tail.replaceAll("(.),?\\1","")) </code></pre> <p>Puzzle is taken from the command line.</p> <p>338 bytes. Not too shabby since this is a statically typed language, and still relatively readable (if you replace ; with newlines)</p> <p>Readable version follows (with more optimal moves)</p> <pre><code>val rods = args(0).split(",", -1); var diskLocation = Map{ { for (rod &lt;-0 to 2 if rods(rod).nonEmpty; n &lt;-rods(rod).split('-').map{_.toInt}) yield(n, rod + 1) }:_* } val nDisks = diskLocation.max._1 var moves = "" def moveTower(start:Int, end:Int, n:Int):Unit = if (n != 0) { val other = 6 - start - end moveTower(start, other, n - 1) moveDisk(n, end) moveTower(other, end, n - 1) } def moveDisk(n:Int, end:Int) = { moves = moves + "," + diskLocation(n) + end diskLocation = diskLocation.updated(n, end); } for (c &lt;- 2 to nDisks) { var firstLocation = diskLocation(1) var nextLocation = diskLocation(c) if (firstLocation != nextLocation) { if (c != nDisks) { val diskAfter = diskLocation(c + 1) if (diskAfter != firstLocation &amp;&amp; diskAfter != nextLocation) { moveDisk(c, diskAfter) nextLocation = diskAfter } } moveTower(diskLocation(1), diskLocation(c), c - 1); } } if (moves == "") moveTower(diskLocation(1), diskLocation(1)%3 + 1, nDisks) println(moves.tail.replaceAll("(.),?\\1","")) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload