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 script.pl 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 bronton.pl
, 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.
202 dionys.pl
208 agrishaev.pl
213 andrey_shitov.pl
229 nikolas_shulyakovskiy_s.pl
243 kyusev.pl
246 andrey_fedorov.pl
258 khripunov.pl
294 bor_vas.pl
303 orlenko_aleksandr.pl
326 evgeniy_kim.pl
338 bronton.pl
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.
#!/usr/bin/perl
($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.
$hi=$h--;
while(--$hi) { # and now traverse vertically
$y+=$u; # up (-1) or down (+1)
$a[$x][$y]=++$n;
$L[$x]=$n;
}
$u=-$u;
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 script.pl 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:
($p,$q)=($w,$h)=@ARGV;
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:
$L[$p]='';
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:
$y=$v=$u=1;
The same for the following code lines, and instead of
$a[$x][$y]=++$n;
$L[$x]=$n;
write
$L[$x]=$a[$x][$y]=++$n;
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:
$d=1;
while ($h * ($i=$w--)) {
. . .
$d=-$d;
}
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 {
$L[$x]=$a[$x][$y]=++$n
}
Instead of
while($wi--) {
$x+=$v;
$L[$x]=$a[$x][$y]=++$n;
}
and
while(--$hi) {
$y+=$u;
$L[$x]=$a[$x][$y]=++$n;
}
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:
$d=1;
while ($h * ($i=$w--)) {
$x+=$d, p while $i--;
$i=$h--;
$y+=$d, p while --$i;
$d=-$d;
}
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:
($w,$h,$y,$z)=(@ARGV)x2;
Althought this is two characters longer than this:
($p,$q)=($w,$h)=@ARGV;
In the Golf announcer's version bronton.pl
there's another interesting method of initialising
variables:
($c,$m,$n)=(1,@ARGV);
Nevertheless it is shorter to make it step by step:
$c=1;($m,$n)=@ARGV;
Nikolay Shulyakovsky exploited
the seldomly used flip-flop operator ...
:
do{$r[$j][$v+($i+=$v)]+1}...do{$r[$v+($j+=$v)][$i]+1and$v*=-1}
Andrey Fedorov changes the direction,
with multiplying by -1
or 0
, making the trick to get it from the current position:
$X*=!!$Y--;$k*=-1;
By the way, multiplying by one is of the same length as an explicit sign change:
$k*=-1;
$k=-$k;
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:
$l=y///c;
In the Eugeniy Kim's solution the corners are being calculated with the pre-filled border matrix:
@r=([1,0],[0,1],[-1,0],[0,-1]);
. . .
($x,$y)=@{$r[++$d%4]}
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)
]]>Did you see that the brand new code editor just released by Microsoft (https://code.visualstudio.com) ships with the Perl 6 syntax support?
]]>You can use search page http://allperlbooks.com/search or the list of books on github: https://github.com/ash/allperlbooks/blob/master/books.yml.
]]>Let me announce my new Perl related site devoted to all the Perl books ever published: allperlbooks.com.
This is a collection of the Perl book covers, primarily from the private library of Liz and Wendy. You might have seen these book shelves at the Perl booth at FOSDEM or at one of the Dutch Perl Workshops.
I would be very appreciated if you will share more Perl book covers with me, my main interest is non-English books (maybe except Russian ones as I have a few dozens of them, and probably almost everything which was published in this language).
For the funny part, here's the page where you'll find a pearl on the book cover :-)
Feedback is also welcome.
]]>Pavel on Twitter
On Foursquare
On YAPC sites
For a few months Pavel has been organising Moscow.pm technical meetings in the office of mail.ru, the company in Moscow he works in. They managed to have two or three talks for every meeting. I think it was a good proof that Pavel was a good candidate for this position and he would bring a new wave to Moscow's Perl life.
I took the leadership in 2007 and was behind a number of Moscow.pm offline meetings, a big enough number of workshops, conferences and hackathons in several countries. Since 2007 our mailing list became de-facto the ExUSSR Perl mailing list. There were a few monger groups appeared in Russia after Moscow.pm relaunch in 2007. As I am no longer living (but not forever) in Moscow and cannot take part in most of its activities, I think we made a good change :-)
I wish the new leader all the best and would like to see newcomers to Perl who will like the language because of Moscow.pm members' work.
]]>This august, in the centre of Kiev, there took place an annual international conference YAPC::Europe 2013. I attended almost no talks, thus can only write about the organisational point of view.
We started the preparations in July 2013. It all begun with a simple question of whether we had to make a YAPC in Kiev.
Question:
From: Andrew Shitov
Date: 2012/7/10
Subject: yapc?
To: Viacheslav Tykhanovskyi, Yaroslav Korshak
Hi!
What do you think about YAPC::EU in Kiev?
Answer:
From: vti
Date: 2012/7/10
Subject: Re: yapc?
To: Andrew Shitov
I only agree! :)
YAPC::Europe moves between European cities. There is a so called Venue Committee, which selects where the next conference takes place. The Committee consists of the organisers of the previous events, and they choose between the proposals submitted by different cities. For the 2013, there were no proposals submitted before the deadline. I did not want to make my second YAPC::Europe, as in that case I would have to make it better than the one in Riga in 2011. But as there are no other proposals, the mission changed. Not only we got the right to host the conference but we also saved the YAPC::Europe and we also saved Perl. (Worth mentioning that there were three proposals for 2014, two of them partially copying the text or the structure of ours.)
There are usually about 300 people coming to YAPC::Europe. We found seven venues in Kiev, which could host them all, having a possibility to make parallel tracks. A few of them were only for an emergency, but two or three were very good, and their price varied substantially. We chose the best one in sense of capacity and location in the very centre of Kiev, thus the following year we spent with communicating to the sponsors :-) Bug having no Ukrainian House, the conference would not be so bright.
The conference itself lasts for three years, although during the last few years the programme tends to be extended up to a week. In our case there were one additional day, Sunday, when we had two events: the Perl 6 hackathon and the pre-conference meeting in a cafe.
The pre-conference meeting took place Sunday evening in the local cafe "Katyusha" close to the central street and not far away from the conference venue. When I found this place and made a preliminary order for 100 people, we published a newsletter asking to register online if you were going to come. The first couples of weeks people were registering very slowly and we thought that the order would be too big. But one week before the conference a burst happened, and we had to double the order. Finally there were 155 people in the list (and not everybody who came had put their name there). Not only we occupied the whole first floor of the cafe but also a terrace nearby.
The YAPC::Europe permanent attendees say that it was quite different before. You could meet only 5-10 people on the conference eve. More and more people come early these days and the pre-conference meetings become bigger and bigger.
About 25 people attended the Perl 6 hackathon, including the leading developers (except Patrick Michaud and Damian Conway who did not attend the conference this year) and "stranger" people who wanted to see the Perl 6 development. The event took place in one of the conference rooms of the Dnipro hotel. The room was adjacent to the hotel restaurant so it was quite convenient place to hack-and-eat.
-- My first question is to Karl Mäsak. What did you do there, what did you achieve, did you complete Perl 6?
-- Yes, we completed it, we just didn't tell anyone. It's always nice to see regulars and new people meeting in a room, and it was such a nice room as well. During the day I saw people work on bugs, work on the channel saying: "Oh, this bug can be closed", or work on the implementation, or write the first lines of Perl 6, or simply sit there and talk about the syntax and semantics. It was a full day of that. Having so many people sort of buzz around of Perl 6 is nice. So, yes, everything from actual compiler hacking to module writing. I think I saw two or three people write a module and upload it somewhere. So, a lot of stuff happen.
Despite all the issues with Perl 6, I am continuing gathering hackathons. For me it is interesting to see people working on documentation and finding out the incompatibilities or rough places there. As for the compiler, its speed and development pace let's talk another time.
The conference theme was 'Future Perl'. On Wensday, I moderated a 40 minute talk-and-panel about that future. I talked about the flame war about Perl 7 happened earlier this year, about my thought of what we can do with it, demonstrated the video fragment with brian d foy, and then called Larry Wall, Liz Mattijsen and Carl Mäsak to the stage. The complete story was video recorded and is available on YouTube. Larry expressed an idea that modern trend in computer languages it to break backwards compatibility with major version change. The good thing is that there is another hope for the burst in Perl 6 development, although I would escape doing any prognosis. Just see the video and think.
It is a tradition to give a T-shirt to the attendees with the name and place of the conference printed on it. This year we implemented an experimental idea and let the people choose not only T-shirt's size but also a colour and the text on it. Nobody did that before. The front side contained "YAPC::Europe, Kiev, Ukraine, 12-14 August 2013" common text, while the back was one of these:
About 200 people made their choice. The sizes, colours and texts gave more than 120 unique combinations, thus this was not a regular order for the printing company. But they did it on time as promised, and on Friday vti and I filled my hotel room with boxes full of T-shirts, trying to sort them and find a few for the hackathon on Sunday.
perl -MData::Dumper -nE'chomp; $t{$_}++; END{say Dumper \%t}'
The most popular text was a slogan use perl or die; (94 people), the second one was '25 years of Perl' (43). All the rest go after a big gap.
This experiment was very interesting and promising, although it was quite a time and labour consuming. The fast increasing number of attendees did not let us printing enough T-shirts but we published the original vector files online so it is very easy now to print any of them.
«The second day of YAPC Europe climaxed in the river boat cruise, Kiev's version of the traditional conference dinner. It was a largish boat traveling on the Dnipro river, with food, drinks and lots of Perl folks. Not having fixed tables, and having to get up to fetch food and drinks led to a lot of circulation, and thus meeting many more people than at traditionally dinners. I loved it.». (Moritz Lenz)
We have already tested the smaller version of the river cruise during one of the previous YAPCs::Russia in Kiev and knew that this action would be very pleasant for everybody. For the 2013 conference we rented the three-dock ship, the biggest one of this kind in Kiev and in the whole Ukraine. The story with the pre-conference order repeated, and we had to increase the order, this time three times. There were about 350 people on the boat: 330 conference attendees and their families (quite a few with children).
If you missed the conference or the cruise, see Dmitry Ivanov's video, the bigger part of it (starting from 5:34) is about the river cruise.
Each of the conference evenings was filled with some entertaining programme. The minus first day had a pre-conference meeting, the second one -- the river cruise, and on the first and the third days there were evening parties sponsored by different companies. First, the party in the venue, then the one in a beer restaurant nearby.
I think this might become a new YAPC::Europe tradition. If we had only one evening filled with the attendees dinner before, we now can ask sponsors to fill others. The attendees will gather together in any case, so why not make this official. (I hope there will be no need to make a day off during the future conferences to give the attendees some rest.)
Something of what happened was either tested or invented by us before. Something we did for the first time. For something we simply had no time. For example, the skeleton of the Riga 2011 conference was care about the speakers. There was a free four-hour Speaker Training organised before the conference. There were a couple of Speaker Rooms in the venue where the speakers could prepare their talks in a quiet room, and even tested their laptops with a projector. This year the background idea was to show how beautiful Kiev is. This is why we had the Partners Programme, the River Cruise and the venue in the city centre. We skipped making the group photo in front of the venue. But there was a completed experiment with publishing weekly newsletters during the whole year of preparation.
This year there were seven(!) volunteers helping us. Three of them (Breno Oliveira from Brasil, Amalia Pomian and Diana Donca from Romania) were working at the check-in desk spreading the name badges and the T-shirts. Another four (Evgeniy Patlan, Vyacheslav Sarzhan, Alexander Kubrack and Mykola Marzhan form Ukraine) made the schedule strict, made all the video recordings and helped with the equipment in the talk rooms. It would be much more difficult for us to make a good conference without them. By the way, video recordings are being added to the youtube.com/yapctv channel.
The next year's YAPC::Europe conference takes place in Sofia, Bulgaria. Another two proposals from Granada, Spain and Cluj-Napoca, Romania that the Venue Committee received, were also great, and I hope that they will be repeated for the 2015.
The conference in Sofia is being organised by Marian Marinov who has been gathering the Bulgarian Perl Workshop during the last five years (ok, we did the first one in 2009 together with him). I encourage everybody to come to Sofia and not miss the chance to meet all the Perl falks in person. The conference theme will be 'Distributed Perl'.
Huge thanks to my colleague Viacheslav Tykhanovskyi (vti) with whom we were preparing and leading this conference, were running around Kiev for different conference stuff, meeting the guests in the airport, reading and writing to Twitter and the conference site, thinking about the process flow, were fixing the schedule deep in the night, and even had have the chance to have a dinner couple of times.
]]>We are proud to announce that we will have the full-time three-day Partner Programme for the partners of the conference attendees.
Our idea is that both the attendee and their partner come together to the venue in the morning, then split, and in the evening they meet again at the venue. Partners participating in the Programme pay only for the tickets to museums etc, or food they consume during their day. No additional fees.
More details and a draft schedule have been just published in this week's newsletter. Feel free to let us your thoughts either by e-mailing to mail@yapcrussia.org or leaving a comment below the news.
]]>First of all, we signed a contract with the river cruise boat and are thinking now of the best possible route so that we can both see the incredible Kiev landscape from water and have enough time for the social part on board. Also, we are working on the buffet menu. All in all, the cruise is going to be an amazing conference experience. Just take a look at their official video.
Let us also remind that less than a month is left until the Early Bird price expires. If you buy a ticket today, you can save 18% of its price. This is only possible before 1 April.
Also don't forget that the number of available rooms in the hotels around the venue can only decrease. Find and book your hotel free of charge. There's also a wiki page "Who stays where" where you can left a note of where you are going to stay so that other attendees can choose a hotel where more (or fewer) Perl people will be staying during the conference.
And now the most important thing. We have asked three persons, all well known in the Perl community, Dmitry Karasik, Alex Kapranov and Ruslan Zakirov, to join us to work in the Programming Committee. So, we have enough eyes and brains now to compile a good schedule with as few thematic clashes as possible. In other words, it is the right moment to submit your talk to the conference. Don't have a talk ready? Submit a proposal and follow the CDD (conference driven development) technique to finish the slides :-)
The title of this year's conference is "Future Perl". We would like to think together about the future of our beloved programming language, about what it will become in the following years, and what is the roadmap.
There are already a couple of talks scheduled for the first day of the conference, not counting Larry Wall's keynote.
Dave Cross. 25 Years of Perl in (about) 25 Minutes.
Richard Jelinek. HowTo: Perl as the most popular scripting language.
That's all for today, see you next week. Would you like to contact us, do not hesitate to send an e-mail to mail@yapcrussia.org.
]]>A couple of days ago we launched an incredible service for the attendees of this year's YAPC::Europe in Kiev.
Book the hotel using our service and do two things at once:
All the details about how it works are published in this week's conference newsletter.
Find your hotel now: act.yapc.eu/ye2013/accommodation/index.html.
Stay tuned. We've got more cool stuff.
]]>Anyway, I do not feel comfortable communicating with an anonymous, even if there's a lot to discuss here, sorry.
]]>