How to improve Ruby code under some restrictions


I'm (still) trying to make another submission to the Largest Number Printable question, and I've got the following bit of code:

f=->a{b,c,d=a;b ?a==b ?~-a:a==a-[c-c]?[[b,c,f[d]],~-c,b]:b:$n}
h=f[h]until h==p($n+=$n)-$n


if b
  if a==b # a is not an array
    return a-1
  elsif a==a-[0] # a does not contain 0
    t = [b,c,f[d]]
    return [t,c-1,b]
  else # a is an array containing 0
    return b
else # if a == []
  return $n

$n = 126
h = [[],n,[]]

until h==0 do
  $n += $n
  p $n
  h = f[h]

The conditions I'm working with:

  • Maximum 100 bytes.

  • No digits allowed in my code.

My code is currently at 108 bytes, but I can't figure out how to golf it further.

The h==p($n+=$n)-$n may be replaced with h==0, but I can't use digits and I still need to print $n and apply $n+=$n in the until loop.

I'm also satisfied with code that replaces $n+=$n with $n+=1, but I can't use digits unfortunately.

Any suggestions?

Simply Beautiful Art

Posted 2017-12-02T19:42:01.077

Reputation: 2 140

It would be helpful if you could include in your question a version of the code with whitespace and comments so we don’t have to figure every bit out on our own. – Jordan – 2017-12-02T21:43:19.417

@Jordan Updated, sorry. – Simply Beautiful Art – 2017-12-02T22:11:12.560



100 bytes

For my purposes, this is good enough.

f=->a{b,c,d=a;b ?a==b ?~-a:a==a-[c-c]?[[b,c,f[d]],~-c,b]:b:p(n+=n)}
h=f[h]until h==n

Replacing h==0 with h==n will not cause the program to fail to terminate, which is what I needed.

Also improves on Jordan's answer by declaring h and n in the same line.

Simply Beautiful Art

Posted 2017-12-02T19:42:01.077

Reputation: 2 140



You can save three bytes by declaring n as a local variable (instead of a global) before f:

f=->a{b,c,d=a;b ?a==b ?~-a:a==a-[c-c]?[[b,c,f[d]],~-c,b]:b:n}
h=f[h]until h==p(n+=n)-n


Posted 2017-12-02T19:42:01.077

Reputation: 5 001

Thanks! For my purposes, this can be written like so, making it 103 bytes.

– Simply Beautiful Art – 2017-12-02T22:12:30.587