# rubyoptima v0.2 - Core War optima number calculator in Ruby by John Metcalf # using the bomb-free space method - 15/02/2024 # Usage: $ irb -r ./optima.rb # >> optima(coresize, modulo) def optima(coresize, modulo = 1) scores = [] (1..coresize / 2).select { |n| coresize.gcd(n) == modulo }.each do |step| score = coresize - 1 high = low = pos = step until pos.zero? pos = (pos + step) % coresize high = [pos, high].max low = [pos, low].min score += coresize - high + low - 1 end scores << [step, score] end puts 'step,score' scores.sort_by(&:last).take(20).each { |i| puts i.join(',') } nil end