How to improve Ruby code under some restrictions
105
You can save three bytes by declaring n
as a local variable (instead of a global) before f
:
n=?~.ord
f=->a{b,c,d=a;b ?a==b ?~-a:a==a-[c-c]?[[b,c,f[d]],~-c,b]:b:n}
h=[],n,[]
h=f[h]until h==p(n+=n)-n
100 bytes
For my purposes, this is good enough.
h=[],n=?~.ord,n
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.