# Estimate ballast and balance for 50:50 distribution in GT4.

def estimate_balance(fb, ow, nw)
    raise ArgumentError unless 0 <= fb and fb <= 100
    raise ArgumentError unless 0 <= ow
    raise ArgumentError unless 0 <= nw

    ballast = (nw - ow).clamp(0, 200)
    balance = 0

    if ballast != 0
        balance = ((fb - 50) * ow / ballast).clamp(-50, 50)
        balance_normal = (balance + 50) / 100.0
        fb_normal = fb / 100.0

        fw = ow * fb_normal + ballast * (1 - balance_normal)
        rw = ow * (1 - fb_normal) + ballast * balance_normal
        fb = 50 * fw / rw
    end

    [ballast, balance, fb.round(2)]
end

def estimate_weight_reduction(w)
    s0 = w
    s1 = w * (1 - 0.09)
    s2 = w * (1 - 0.12)
    s3 = w * (1 - 0.15)
    [s0, s1, s2, s3].map(&:to_i)
end

def show(fb, iw)
    ws = estimate_weight_reduction(iw)
    ws.each do |ow|
        nw = ow + 200
        printf("%d:%d:%d %p\n", fb, ow, nw, estimate_balance(fb, ow, nw))
    end

    ws.drop(1).each do |ow|
        nw = iw
        printf("%d:%d:%d %p\n", fb, ow, nw, estimate_balance(fb, ow, nw))
    end
end

# ..

puts "Nissan Skyline GTS-R (R31) '87"
show(60, 1340)
puts "Nissan Skyline GTS25 Type S (R32) '91"
show(55, 1320)
puts "Lotus Esprit V8-GT '98"
show(43, 1325)