4 lambda version
source | link

Ruby, 51

a=eval$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}

Run from command line with [array,R] as input. It shows each increment of R along the way. The last line is the solution.

For example, if the array is [3,1,2] and R=7:

$ ruby -ne 'a=eval$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}' <<< [3,1,2,7]
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer

For the same char count but different calling convention:

lambda{|a,r|r.times{a<<j=0;a.map!{|i|(v=j)+j=i}};a}

Ruby, 51

a=eval$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}

Run from command line with [array,R] as input. It shows each increment of R along the way. The last line is the solution.

For example, if the array is [3,1,2] and R=7:

$ ruby -ne 'a=eval$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}' <<< [3,1,2,7]
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer

Ruby, 51

a=eval$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}

Run from command line with [array,R] as input. It shows each increment of R along the way. The last line is the solution.

For example, if the array is [3,1,2] and R=7:

$ ruby -ne 'a=eval$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}' <<< [3,1,2,7]
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer

For the same char count but different calling convention:

lambda{|a,r|r.times{a<<j=0;a.map!{|i|(v=j)+j=i}};a}
3 golfed off 18 chars
source | link

Ruby, 6951

a=eval$_;a.pop.times{a=[0]+a;a.sizea<<j=0;p a.timesmap!{|i|a[i]+=|i|(a+[0]v=j)[i+1]+j=i};p a}

Run from command line with [array,R] as input. It shows each increment of R along the way. The last line is the solution.

For example, if the array is [3,1,2] and r=7R=7:

$ ruby -ne 'a=eval$_;a.pop.times{a=[0]+a;a.sizea<<j=0;p a.timesmap!{|i|a[i]+=|i|(a+[0]v=j)[i+1]+j=i};p a}' <<< '[3[3,1,2,7]'7]
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer

Ruby, 69

a=eval$_;a.pop.times{a=[0]+a;a.size.times{|i|a[i]+=(a+[0])[i+1]};p a}

Run from command line with [array,R] as input. It shows each increment of R along the way. The last line is the solution.

For example, if the array is [3,1,2] and r=7:

$ ruby -ne 'a=eval$_;a.pop.times{a=[0]+a;a.size.times{|i|a[i]+=(a+[0])[i+1]};p a}' <<< '[3,1,2,7]'
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer

Ruby, 51

a=eval$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}

Run from command line with [array,R] as input. It shows each increment of R along the way. The last line is the solution.

For example, if the array is [3,1,2] and R=7:

$ ruby -ne 'a=eval$_;a.pop.times{a<<j=0;p a.map!{|i|(v=j)+j=i}}' <<< [3,1,2,7]
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer
2 edited body
source | link

Ruby, 69

a=eval$_;a.pop.times{a=[0]+a;a.size.times{|i|a[i]+=(a+[0])[i+1]};p a}

Run from command line with [array,R] as input. It shows each increment of rR along the way. The last line is the solution.

For example, if the array is [3,1,2] and r=7:

$ ruby -ne 'a=eval$_;a.pop.times{a=[0]+a;a.size.times{|i|a[i]+=(a+[0])[i+1]};p a}' <<< '[3,1,2,7]'
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer

Ruby, 69

a=eval$_;a.pop.times{a=[0]+a;a.size.times{|i|a[i]+=(a+[0])[i+1]};p a}

Run from command line with [array,R] as input. It shows each increment of r along the way. The last line is the solution.

For example, if the array is [3,1,2] and r=7:

$ ruby -ne 'a=eval$_;a.pop.times{a=[0]+a;a.size.times{|i|a[i]+=(a+[0])[i+1]};p a}' <<< '[3,1,2,7]'
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer

Ruby, 69

a=eval$_;a.pop.times{a=[0]+a;a.size.times{|i|a[i]+=(a+[0])[i+1]};p a}

Run from command line with [array,R] as input. It shows each increment of R along the way. The last line is the solution.

For example, if the array is [3,1,2] and r=7:

$ ruby -ne 'a=eval$_;a.pop.times{a=[0]+a;a.size.times{|i|a[i]+=(a+[0])[i+1]};p a}' <<< '[3,1,2,7]'
[3, 4, 3, 2]
[3, 7, 7, 5, 2]
[3, 10, 14, 12, 7, 2]
[3, 13, 24, 26, 19, 9, 2]
[3, 16, 37, 50, 45, 28, 11, 2]
[3, 19, 53, 87, 95, 73, 39, 13, 2]
[3, 22, 72, 140, 182, 168, 112, 52, 15, 2]   <-- answer
1
source | link