Perl Golf at YAPC::Russia 2015

The after-story about the Perl Golf contest, which took place during the two-day YAPC::Russia conferece in Moscow

Perl Golf is a contest, where participants solve the task with the shortest Perl programme possible. This year's Golf rules were announced by Vadim Pushtaev right after the conference start.

You can find a formal desciption and all the received solutions in the YAPC_Russia_2015_perl_golf GitHub repository.

The task. The script takes two parameters, matrix's width and height, and within the given rectangular field prints a series of incremented numbers starting by 1, going along the spiral towards the centre of the field:

$ perl 7 6
1  2  3  4  5  6  7
22 23 24 25 26 27 8
21 36 37 38 39 28 9
20 35 42 41 40 29 10
19 34 33 32 31 30 11
18 17 16 15 14 13 12

The columns are left-aligned and are separated by exactly one space.

The minimal demo solution, which was a part of the task, contained 338 symbols, and it was of no interest to submit anything longer. It was funny to watch the lengths of the different solutions that the participants were submitting during the days of the conference. Once, there were three files, of 244, 245 and 246 symbols each, and there was a feeling that those candidates could win by only removing one or two characters in their programmes. But a few hours before the deadline,
Denis Ibaev submitted his version, which was 205-symbol long, and although he was the absolute leader, Denis managed to make the code even shorter (202 chars). For me it's extremely notable that the whole script was written on an iPad.


I'll describe my solution (the readers can explore other solutions themselves :-).

First of all, I had to understand how to make the spiral in the most optimal way. I wanted to create or find a formula, which translates cell coordinates into the value. In the internets, you can find the Ulam spiral --- same as in the Golf task but rolling in opposite direction --- and even some kind of a formula I needed but I rejected that path because it was not clear how to calculate the number of spaces between columns. Having the formula, it was tempting to immediately print the result, skipping the second loop for printing.

Anyway, two steps were done: generating and printing. To make the spiral, you need to turn at the corners, keeping the already filled cells unchanged. I drew a few matrices on paper and found out that each time the spiral makes its turn the length of the next segment decrements by 1. In other words, for the 7×5 matrix the first two segments are 7 and 4 steps, the next two are 6 and 3, then 5 and 2, etc.

All the above gave the following initial script.


($w,$h)=@ARGV; # width and height

exit if !($w*$h);
#$n=0; # cell value
#$x=0; # initial horisontal position

$y=1; # initial vertical position

$v=1; # vector of moving horisontally
$u=1; # and vertically

# main loop - by width
while ($wi=$w--) { # the above-mentioned decrement of a segment 
    while ($wi--) { # before spiral turn
        $x+=$v; # moving one cell right or left
        $a[$x][$y]=++$n; # fixing the result
        $L[$x]=$n; # @L is a one-dimentional array, one value for each line,
                   # it is being filled with the currently generated value,
                   # thus when the matrix is filled, 
                   # @L will contain values with maximum possible length
                   # in each column, thus we know the column width. 

    $v=-$v; # preparing for the next horizontal segment,
            # changing direction.

    while(--$hi) { # and now traverse vertically
        $y+=$u;    # up (-1) or down (+1)
    last if !$h;

($w,$h)=@ARGV; # initial values are already broken, re-read them

# simple loop printing @a
for $y (1..$h) {
    for $x (1..$w) {
        # the only less obvious part is 
        # the formatting string like "%-5i" with the length from @L
        $s = $x != $w ? 1+length $L[$x] : '';
        printf "%-${s}i", $a[$x][$y];
    print "\n";

Interestingly, the solution of Denis Ibaev does not contain two-dimentional matrix, and all the values are kept in a bare array.

It became obvious that it is not possible to determine the width of the column as 1 + length $w*$h, because it might occur that there will be only one long number, and it will break the whole picture making one of the columns one char wider than others.

$ perl 10 10
1  2  3  4  5   6  7  8  9  10
36 37 38 39 40  41 42 43 44 11
35 64 65 66 67  68 69 70 45 12
34 63 84 85 86  87 88 71 46 13
33 62 83 96 97  98 89 72 47 14
32 61 82 95 100 99 90 73 48 15
31 60 81 94 93  92 91 74 49 16
30 59 80 79 78  77 76 75 50 17
29 58 57 56 55  54 53 52 51 18
28 27 26 25 24  23 22 21 20 19

That's why the @L array was introduced.

Now we come to some optimisations. First of all, it is possible to gain a few characters when re-reading the arguments:


Also, it is wise to remove any data checks because the Golf test script did not use any incorrect initial values :-) Zero or negative width or height potentially can create infinitive loops but we don't care.

exit if !($w*$h);

The column length is determined by the width of the number contained in the corresponding element of the @L array. To get rid of trailing spaces we can make the length of the last element zero:


This allows to exclude the check for the final column:

$s = $x != $w ? 1+length $L[$x] : '';

Now it would be nice to get rid of the condition to stop the loop. The loop itself looks like this:

while ($wi=$w--) {
   . . .
   last if !$h;

Instead, you can say this:

while ($h * ($wi=$w--)) {
   . . .

Variable initialisation (those which should start with a non-zero value) can be done in one turn:


The same for the following code lines, and instead of




Now a small but useful optimisation of the print loop by introducing the postfix form of for:

for $y (1..$q) {
    printf "%-".(1+length$L[$_])."i", $a[$_][$y] for 1..$p;
    print "\n";

Then it came to my mind that the $u and $v variables both change at the same time: it never happens that you use one of them before the second one has changed. Even more, both of them are always either -1, or 1. So, we can replace them with a single $d variable, altering its sign at the end of the external loop:


while ($h * ($i=$w--)) {
    . . .

The nested while loop also could be modified to become postfix but first let's try factoring out some repeating code, which was a good idea:

sub p {

Instead of

while($wi--) {


while(--$hi) {

the code looks like this now:

$x+=$d, p while $i--;

. . .

$y+=$d, p while --$i;

It was very promising to make the above decrements $i-- and --$i both either prefix or postfix and remove even more duplicated code but it was not that easy to achieve. Instead, I spent some time for an attempt of creating a function changing either$xor$y`:

#sub e {
#   eval "\$$_[0]+=$d, p while $_[1]"

. . .

#e 'x','$i--';
. . .
#e 'y','--$i';

The result was longer than before, thus this modification had to be rejected.

The local failure was followed by a successful change, which made the initial value of $y zero instead of 1, and the $y = 0 instruction is obviously redundant for the Golf script.

So, the final spiral generation loop is this:

while ($h * ($i=$w--)) {        
    $x+=$d, p while $i--;
    $y+=$d, p while --$i;

And one more thing to change in the printing code. First I gain one char by replacing the \n with a real newline character:

print "

But then, after reading perldoc perlop and Perl Golf 101, I discovered that the golfers have already invented using the $/ special variable instead of "\n".

Finally, a small but useful experiment by replacing string concatenation

printf "%-".(1+length$L[$_])."i"

with interpolation with embedded code "@{[]}":

printf "%-@{[1+length$L[$_]]}i"

Done. 213 symbols:

sub p{$L[$x]=$a[$x][$y]=++$n}($p,$q)=($w,$h)=@ARGV;$d=1;while($h*($i=$w--))
{$x+=$d,p while$i--;$i=$h--;$y+=$d,p while--$i;$d=-$d}$L[$p]='';
for$y(0..$q-1){printf"%-@{[1+length$L[$_]]}i",$a[$_][$y]for 1..$p;print$/}

A few words about other interesting stuff that you can find in the solutions of other participants.

Alexander Orlenko duplicated the initial parameters:


Althought this is two characters longer than this:


In the Golf announcer's version there's another interesting method of initialising variables:


Nevertheless it is shorter to make it step by step:


Nikolay Shulyakovsky exploited the seldomly used flip-flop operator ...:


Andrey Fedorov changes the direction, with multiplying by -1 or 0, making the trick to get it from the current position:


By the way, multiplying by one is of the same length as an explicit sign change:


Andrey also found the way to make printf's format shorter and used the %-*d format string, passing the length in a separate argument, thus getting rid of string interpolation:

printf'%-*d ',$s[$_],$$B[$_]for 0..$w-2;

Sergey Khripunov used a useful hack to gain characters by avoiding the length keyword:


In the Eugeniy Kim's solution the corners are being calculated with the pre-filled border matrix:

. . .

Anatoly Grishaev first creates the dummy matrix and then fills it with numbers:

($a,$b)=@ARGV;$_=('1 'x$a++."0\n")x$b;@s=split;s/\d/$z++/ge;

(Originally published in Russian in the Pragmatic Perl magazine #28)

Leave a comment

About Andrew Shitov

user-pic I blog about Perl.