Skip to content

Instantly share code, notes, and snippets.

@takkkun
Created February 9, 2013 23:50
Show Gist options
  • Save takkkun/4747633 to your computer and use it in GitHub Desktop.
Save takkkun/4747633 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
OFFENCE = 150 # 自分の攻コスに変更
DEFENCE = 150 # 自分の守コスに変更
# 手持ちのアイドルを並べてく
IDOLS = [
['[孤高の歌姫] 如月千早+', 13516, 10972, 14], # [名前, 攻, 守, コスト] の順で書く
['[麗しの花嫁] 和久井留美+', 8891, 6416, 10],
['[博識子女] 古澤頼子+', 10493, 10493, 12],
# ...
]
class Idol
def initialize(name, offence, defence, cost)
@name = name
@offence = offence
@offence_rate = offence / cost
@defence = defence
@defence_rate = defence / cost
@cost = cost
end
attr_reader :name, :offence, :offence_rate, :defence, :defence_rate, :cost
def actual(field, cost = @cost)
raise ArgumentError unless [:offence, :defence].include?(field.to_sym)
__send__(field) * cost / @cost
end
end
IDOLS.map! { |fields| Idol.new(*fields) }
def print_idols(idols, field, cost_limit)
idols = idols.sort_by { |idol| -idol.send("#{field}_rate") }
total_value = 0
total_cost = 0
border_index = -1
idols.each_with_index do |idol, index|
value = idol.send(field)
rate = idol.send("#{field}_rate")
if (total_cost + idol.cost) <= cost_limit
total_value += idol.actual(field)
elsif total_cost < cost_limit
total_value += idol.actual(field, cost_limit - total_cost)
border_index = index
else
border_index = index
end
print " #{rate.to_s.rjust(3)}: #{idol.name}"
if (total_cost + idol.cost) == cost_limit
border_index = index
puts
break
elsif border_index >= 0
puts " (use #{cost_limit - total_cost}/#{idol.cost})"
break
end
puts
total_cost += idol.cost
end
if border_index >= 0
puts '-' * 80
idols[border_index + 1, 5].each do |idol|
rate = idol.send("#{field}_rate")
puts " #{rate.to_s.rjust(3)}: #{idol.name}"
end
end
puts total_value
border_index >= 0 ? idols[(border_index + 1)..-1] : []
end
puts 'Offensive idols:'
offensive_unnecessary_idols = print_idols(IDOLS, :offence, OFFENCE)
puts
puts 'Defensive idols:'
defensive_unnecessary_idols = print_idols(IDOLS, :defence, DEFENCE)
unnecessary_idols = offensive_unnecessary_idols & defensive_unnecessary_idols
unless unnecessary_idols.empty?
puts
puts 'Unnecessary idols:'
unnecessary_idols.each { |idol| puts " #{idol.name}" }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment