I coded this from a puzzle a friend showed to me.
#!/usr/bin/perl -w use strict;undef $/;$_=<>;while(m+^(\d)(\1*)+) {print length($1.$2),$1;s;$1$2;;;} ######################################################## # # This works best with the numbers 1, 2, and 3. # ########################################################

Replies are listed 'Best First'.
Re: 123 Digit Counter
by Juerd (Abbot) on Mar 13, 2002 at 20:56 UTC

    What exactly does/should it do? Could you give an example please? (I put in some numbers, and got output, but have no idea what it means)

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      It's an encoding of a puzzle, of which one partial series is:
      121 111211 311221 13212211
      The trick is to find out what the rule is.
      Rob
      --
      Nar! Pushdug ghashnurbhar! ($#@! Stinking pile of code!)
        Each new number is composed of digit pairs which count the consecutive occurences of digits in the last number:

        1 2 1 -> 11 12 11 (one '1', one '2', one '1')
        111 2 11 -> 31 12 21 (three '1's, one '2', two '1's)
        3 11 22 1 -> 13 21 22 11 (one '3', two '1's, two '2's, one '1')

        so the next number should be:

        1 3 2 1 22 11 -> 11 13 12 11 22 21 = 111312112221

        I first encountered this sequence as a method of compressing long strings of repeated numbers:

        888888884444444999222222211111116644444 ->
        88743972712654

        Though obviously it's not a good choice for compressing something like:

        123456789123456789 ->
        111213141516171819111213141516171819

        chr0
        ----
        perl -le'$$=substr(%{*::}->{_},2,1);print+(map{$$++for(1..$_);$$}(2,5,10)),$|'

        The trick is to find out what the rule is.

        And what would the "rule" in your example be? And why?

        I really haven't the slightest idea what this is, and what it is supposed to do, but it looks interesting

        U28geW91IGNhbiBhbGwgcm90MTMgY
        W5kIHBhY2soKS4gQnV0IGRvIHlvdS
        ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
        geW91IHNlZSBpdD8gIC0tIEp1ZXJk