ProofExcept for the first line, each line is of the form "C N C N C N", where C denotes the count, and N denotes the number of that count. You can extrapolate the entire pattern forwards and backwards from any given line: "3 1 2 2 1 1" was preceeded by "1 1 1 2 2 1" and is followed by "1 3 1 1 2 2 2 1".

Before a line can contain 4 as a number, it must contain 4 as a count. This requires a line that contains one of these two patterns: "C X X X X N", or "N X X X X C", where X is the same number each time.

First Case: C X X X X N C is the count for the first X, and then X acts as both its own count and the number, and then the last X is the count of N. However, "C X X X" can not occur, since C X's and X X's should be replaced by (C+X) X's. Therefore, the pattern "C X X X X N" can not occur.

Second Case: N X X X X C Both X X pairs are a count-number pair. This is impossible since X X's and X X's should be (X+X) X's. Therefore, the pattern "N X X X X C" can not occur.

The number 4 will never be used in this pattern. QED

Extrapolation: since 4 consecutive repeated numbers can never occur, this pattern can never contain more than 4 consecutive repeated numbers, therefore, this pattern only contains the numbers 1, 2, and 3.

Indeed japhy - that's more the kind of solution I was hoping for I know there are many ways to do things in Perl, but I was wondering where you were going with all the shifting and pushing - it seemed a lot less "Perl-ish" than the regex Cameron used. Of course, I hadn't thought of using that conditional mapping...

Anyway, since this is supposed to be a learning experience for everyone, I'd like to point out one little problem with the solution Cameron gave : it uses recursive calls to P($new), and that's probably not recommended here.

My second solution was faster then the others (and yes, I did change the 1..6 to a 1..10). I optimized Cameron's to be a { ...; redo } block instead of a function call, since recursion can slow you down.