Fast Topswops calculation

11

From AZSPCS:

Suppose you have a deck containing n cards. Each card contains a number from 1 to n, and each number appears on exactly one card. You look at the number on the top card -- let's says it's k -- and then reverse the order of the top k cards. You continue this procedure -- reading the top number and then reversing the corresponding number of cards -- until the top card is 1.

Write the fastest program to compute the number of reversals for a given deck. Note that if you are participating in the contest you are not allowed to post your code (and thus I will not post my code yet).

Alexandru

Posted 2011-01-27T23:00:59.460

Reputation: 5 485

What is the input/output model? Any language restrictions? How will you determine how fast each entry is? – aaaaaaaaaaaa – 2011-01-28T02:24:07.010

So are we allowed to post solutions or not? – AShelly – 2011-02-09T16:25:10.380

Yes. The contest has finished. – Alexandru – 2011-02-13T11:42:44.593

There could be a dedicated stackexchange for azspcs ;) – Eelvex – 2011-01-30T23:22:44.457

The link to azspcs links to a page which is out of order. And it seems a meta-tag, which doesn't describe the puzzle. The tag should, perhaps, be removed. – user unknown – 2011-05-01T01:42:47.020

Answers

5

JavaScript

function(d){for(t=0;x=(n=d[0])-1;t++)for(i=0;i<n/2;i++){m=d[x-i];d[x-i]=d[i];d[i]=m}return t}

You pass it the deck, like so:

f([3, 2, 1]) // 1
f([2, 3, 1]) // 2
f([1, 2, 3]) // 0

Ry-

Posted 2011-01-27T23:00:59.460

Reputation: 5 283

So you're the winner! :) – user unknown – 2011-05-01T01:33:44.270

3

Scala: (This isn't a golf - is it?)

def transform (l: List[Int], sofar: Int = 0) : Int =
  if (l(0) == 1) sofar else transform (l.take (l(0)).reverse ::: l.drop (l(0)), sofar + 1)

Complete application with testcase and stopwatch, including the shuffling of the Deck:

object DeckReverse extends Application {

  def transform (l: List[Int], sofar: Int = 0) : Int = 
    if (l(0) == 1) sofar else transform (l.take (l(0)).reverse ::: l.drop (l(0)), sofar + 1)

  def stopwatch (count: Int, size: Int) = {
    val li = (1 until size).toList 
    val r = util.Random 

    val start = System.currentTimeMillis ()
    (0 until count).foreach (_ => transform (r.shuffle (li)))
    val stop = System.currentTimeMillis ()

    println ("count: " + count + "\tsize: " + size + "\tduration: " + (stop - start) + " msecs") 
  }

  stopwatch (1000, 100)
}

count: 1000 size: 100 duration: 1614 msecs machine: Single Pentium M 2Ghz

user unknown

Posted 2011-01-27T23:00:59.460

Reputation: 4 210

2

Python, 84 Chars

Golfing anyway... I'm using the numbers 0 through n-1. Assuming the array is stored in a variable x, it takes me 84 chars of Python.

while x[0]:x[:x[0]+1]=x[x[0]::-1]

However, performance is pretty bad due to memory abuse.

boothby

Posted 2011-01-27T23:00:59.460

Reputation: 9 038

0

C

int revno(int* deck, int n) {
  int k,j,r=0;
  for(;;) {
    k=*deck;
    if (k==1) {
      return r;
    }
    for (j=0; j<k/2; j++) {
      int tmp=deck[j];
      deck[j]=deck[k-j];
      deck[k-j]=tmp;
    }
    r++;
  }
}

deck is a pointer to an integer array representing the decks. n is the number of the cards. Obviously memory safety is the task of the caller.

It is probably nearing the fastest algorithm on recent computers and on a high-level language. Only with asm-level tricks could it be made faster, but not heavily even with them.

peterh - Reinstate Monica

Posted 2011-01-27T23:00:59.460

Reputation: 347

0

Perl 5, 58 + 2 (-ap) = 60 bytes

++$\&&(@F[0..$F[0]-1]=reverse@F[0..$F[0]-1])while$F[0]-1}{

Try it online!

Xcali

Posted 2011-01-27T23:00:59.460

Reputation: 7 671