g | x | w | all
Bytes Lang Time Link
150Ruby250618T021538ZValue In

Ruby, 150 bytes

Just a quick implementation of the pseudocode to get things started with a few tweaks:

  1. Since all code has that constant +3, we just skip adding it. The length of code for each factor relative to each other stays the same regardless and we just want the factor, not the length of the final code.
  2. Instead of removing trailing zeroes, it saved bytes to just calculate what the length would be by grabbing the index of the last nonzero element and adding 1. Except in 2/3 of the cases where the +1 would be used just results in a constant +3 that we can also cut! For the last case, i+~s saves a byte over i-s-1.
->a,i{z=->b{b.rindex{_1>0}||-1}
(r=0..a.max).min_by{|f,*m|d=a.map{|n|m<<(n-f*c=r.min_by{(n-_1*f).abs}).abs;c}
f+d.sum+2*z[d]+m.sum+(i+~s=z[m]).abs+s}}

Attempt This Online!