[LRUG] ShRUG Golf update

James Adam james at lazyatom.com
Thu May 12 02:44:43 PDT 2011

I thought that I left with is how we always seemed driven towards
functional rather than procedural solutions. This could just be a
consequence of the particular problems we had to solve, which were
almost all simple input transformations, but I suspect the shortest
solution to any problem would include a lot of functional operations.
They really are powerful.

In hindsight this seems obvious, but by the end of the evening I was
desperately trying to think of how to save characters by aliasing
`inject` to something, since we used it in almost every single hole.

Oh, also: a single-letter constant is one character shorter than "-1" :)

- James

On 12 May 2011 10:41, Aanand Prasad <aanand.prasad at gmail.com> wrote:
> 1. The (a ? b : c) ternary operator does the expected thing if you chain it
> and leave out parentheses, making it shorter than if/elsif/else, even if it
> looks terrifying:
> s > 3600 ? "more than an hour" : s > 60 ? "more than a minute" : s > 1 ?
> "more than a second" : "GET OUT OF THE WAY"
> 2. a[-1] is 1 character shorter than a.last.
> On Thursday, May 12, 2011 at 10:30 AM, Murray Steele wrote:
> I think it might be useful to do a bit of a review of things people learned
> while doing rubygolf*.  We didn't have time on the night and it sounds like
> that might have been at good part of the SHRUG meeting.
> I'll start with some snippets that I learned / remembered:
> 1. Enumerable#inject doesn't need a block, it actually takes 2 params, a
> initial value and a symbol as an argument.  Each iteration the symbol is the
> method that'll be called on the accumulator and passed the current element.
>  Also if you don't supply an initial element then it'll use the first
> element of the enumerable.  So you can turn
> array.inject(0) {|s, i| s + i}
> into
> array.inject(:+)
> 2. Procs and Lambdas can be invoked via .[] rather than .call.
> 3. There are millions of ways of defining methods on something.  Luckily,
> the tests don't say that Golf needs to be a class, so we can define Golf as
> a constant and just add methods to it.  So we can turn:
> class Golf
>   class << self
>     def hole1
>       ...
>     end
>   end
> end
> into:
> class << Golf = ''
>   def hole1
>     ...
>   end
> end
> (e.g open up the metaclass of Golf, which happens to be a constant of an
> empty string, rather than a class.  Works because assignment (Golf = '') has
> higher precendence than class-opening-up (class << Golf).
> I tried to shave 1 character by setting Golf to be numbic constant:
> class << Golf = 1
>   def hole1
>     ...
>   end
> end
> But it doesn't work because Fixnums have no metaclass.  Depending on what
> you do to try and add the methods you get various errors like:
> TypeError: no virtual class for Fixnum
> or
> TypeError: can't define singleton method "hole1" for Fixnum"
> I found this surprising, (and I think I've come across it before, and was
> surprised then too) as I always believed the "everything is an object"
> mantra of Ruby, assuming everything was the same under the hood.  Turns out
> that while it's still true that everything is an object, some objects are
> clearly more objecty than others.
> I doubt I'd ever want to use any of these tricks in production code (as
> someone said, it's can be far from readable), but I don't think it's been
> just a toy exercise.  In trying to shave characters off my solution I've
> been pushing the boundaries of what Ruby can and can't do.  This in turn
> helps solidify my understanding of the object-model and syntax parser.
> So, anyone else want to share what they learned?
> Muz
> *: I'm still working on mine and while I don't want to look directly at
> other solutions, I'm happy to be given inspiration by discussing
> techniques**.
> **: Yes I know this makes no sense.
> On 10 May 2011 15:03, Andrew McDonough <andrew at andrewmcdonough.co.uk> wrote:
> Thanks James.  I'm glad that ShRUG enjoyed it.  We were a bit short on
> time at LRUG, with less than an hour available for coding.  Four teams
> completed the course, and our winner was Tomasz Wegrzanowsk (@t_a_w),
> who was working on his own, and managed 704 characters
> (http://j.mp/lbcXV2).  Second place was @zuppr, @jamiemill @morticed
> and @aanand who came a close second in with 843 chars
> http://j.mp/mBjfJU, third place was  @danlucraft, @nfelger, Jordi,
> Dan, Tony, Steve and Keith who completed the course in 883 chars
> http://j.mp/jHhbWU and finally one other anonymous team completed the
> course with 1208 characters (if this was you, please let us know who
> you are).  The winner, Tomasz (http://j.mp/lSsmJd), was presented with
> a trophy (http://j.mp/iRFwGP) and the second placed team got medals.
> There are still four medals left, which I will award at the next LRUG
> to the four people who produce the best solutions (not necessarily the
> shortest ones), so there's still something to play for if you didn't
> come along on the evening.
> There appears to have been a lot of work done since to get the
> character counts down even further, in particular from Paul Battley
> with some help from Tom Stuart.  Their commit log is worth looking
> through:
> https://github.com/threedaymonk/rubygolf/commits/master
> They got the solution down to 654 characters, before realising they
> could exploit my "whitespace isn't counted" rule to encode their
> solution in spaces and newlines, and then simply decode and exec.
> This brought the solution down to 37 characters.  He gets one of the
> four medals.  Genius:
> https://github.com/threedaymonk/rubygolf/blob/master/lib/golf.rb
> I have put the introductory presentation I made up on heroku:
> http://rubygolf-presentation.heroku.com
> Skills Matter have published some photos of the evening:
> http://www.flickr.com/photos/skillsmatter/5704350908/in/set-72157626683669388/
> I will follow up later with some analysis of the solutions and some of
> the tricks and techniques used, but for now, you can look through the
> various solutions that were submitted:
> https://github.com/andrewmcdonough/rubygolf/network
> Congratulations to ShRUG - you guys did really well completing the
> course with so few characters.
> Andrew
> ---
> Andrew McDonough
> CTO, Tribesports
> http://tribesports.com
> On 10 May 2011 13:59, James Almond <james at jamesalmond.com> wrote:
>> Hi all,
>> As promised, an update on ShRUG's progress with the Ruby Golf night we ran
>> in parallel with LRUG.
>> Firstly, I managed to fork the repository before the 7th hole was added
>> and
>> nobody noticed until a fair amount of time into the evening so we
>> completed
>> the course without it. We also worked with a slightly lighter and RSpec
>> 2 compatible runner, but all specs and counting mechanisms stayed the
>> same.
>> Everyone had 1.9.2 installed and favoured it, so we went with that over
>> 1.8.7. I think we were still pretty close to what you were running. We had
>> around 5 pairs of 2.
>> Our lowest team effort on the night was 592 (with hole 7
>> missing): https://github.com/ianwhite/rubygolf/blob/master/lib/golf.rb
>> A group discussion and edit took the count down to
>> 522: https://github.com/shruggers/rubygolf/blob/ecd0b312bfb9707b6debfc01062eab5e39d5f95a/lib/golf.rb
>> A bit of post-ShRUG editing and adding hole 7 our final team count was
>> 604: https://github.com/shruggers/rubygolf/blob/master/lib/golf.rb
>> The discussion and collating of the results at the end was good fun and
>> also
>> made it one of the latest running ShRUG meetings I can remember.
>> Everyone had a great evening, and learnt something new. Thanks to Andrew
>> (and anybody else who contributed) for the holes, much appreciated! The
>> #rubygolf radio silence from Sheffield was because everyone was too busy
>> attempting the holes, sorry about that.
>> James
>> _______________________________________________
>> Chat mailing list
>> Chat at lists.lrug.org
>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org

More information about the Chat mailing list