So TheRubyGame was just brought back from the dead. The newest challenge (Challenge #1) is about ducks and fire and ducks on fire.
Actually the newest challenge is a Fizz buzz challenge.
The fun – and sometimes educational – part here is golfing, that is creating the shortest Ruby script that can solve the challenge.
When we count the shortest, we count only the content of the method.
Here the method
fireducker is given, and thus all of
is given and can’t be changed.
One solution, rather short (70 characters), could be
This is the straightforward approach.
For each of
[3,5,15] change the value to the count of integers between
Call the last
c and remove that count from the two others (all numbers that are in the
c “bucket” will also be counted towards both the
a and the
Lets golf a bit. It is fairly simple to reach 57 characters. We refactor a bit and end up with something like (this is nbaums original shortest solution)
This can actually be golfed even shorter (56 characters, 1 character shorter)
We refactored the division into a
Proc and call this with the
#call shorthand of
Can we make it shorter?
We can take advantage of Ruby returning the value of a variable once we set it.
c=g will return
g and still set
We can do something like (54 characters):
I can’t get it shorter now, however I can make it even more obfuscated. Ruby will of course return the last value in the method. We are suppose to give back an array, however, we can actually give back a variable, which is an array.
Ruby will assign our variable with an array of values, if we do not give enough variables for multi-variable-assignment.
That is, the value of
a = 1, 2 will be
[1, 2] and thus we can – with the help of ✔, Ω, ☃ and ℧ (who needs variable names?) and the fact that Ruby 2.0 is in all unicode – do (54 characters):