Created
November 4, 2012 11:26
-
-
Save lithtle/4011476 to your computer and use it in GitHub Desktop.
elona、羽の生えた巻物シミュレータ?のようなもの
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Description: | |
羽の生えた巻物のシミュレーションをします。 | |
4.3s の刀を二刀流にしたかった際 3.0s と 1.5s までの計算が面倒だったので作りました。 | |
羽の生えた巻物無駄にしたくないですし。 | |
Requirement: | |
ruby 1.8.7 とか 1.9.2 とか | |
Usage: | |
コマンドラインから | |
$ ruby calc_weight.rb [今の重さ] [目標にしたい重さ] | |
とかすると結果がでてきます。 | |
Note: | |
・呪われた羽の生えた巻物使って重くしてぎりぎりを狙おうとか考えていません。 | |
Example: | |
$ ruby calc_weight.rb 4.0 1.5 | |
{"weight"=>1.4945346000000002, "trial"=>["祝福羽", "祝福羽", "羽", "羽", "羽"]} | |
{"weight"=>1.3714552800000002, "trial"=>["祝福羽", "祝福羽", "祝福羽", "羽", "羽"]} | |
{"weight"=>1.2585119040000001, "trial"=>["祝福羽", "祝福羽", "祝福羽", "祝福羽", "羽"]} | |
{"weight"=>1.3843605749999999, "trial"=>["祝福羽", "羽", "羽", "羽", "羽", "羽"]} | |
{"weight"=>1.2703544100000002, "trial"=>["祝福羽", "祝福羽", "羽", "羽", "羽", "羽"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby -Ku | |
# -*- coding: utf-8 -*- | |
# なるべく少ない回数で指定した重さのものをある一定の重さ(>=1.0)以上にするのに | |
# 必要な羽の生えた巻物の試行回数と重さを計算する | |
# 祝福羽の生えた巻物と通常時の巻物の重さ軽減倍率 | |
RATIO = { | |
"blessed" => 0.78, | |
"normal" => 0.85 | |
} | |
# 倍率の組み合わせを計算する配列を返す | |
# === Args | |
# all : 配列の長さ | |
# i : 祝福羽の巻物の回数 | |
# === Returns | |
# 組み合わせの配列 | |
# === Example | |
# add_ratio(3, 1) | |
# => [0.78, 0.85, 0.85] | |
# add_ratio(3, 2) | |
# => [0.78, 0.78, 0.85] | |
def add_ratio(all, i) | |
ret = [] | |
i.times{ ret << RATIO["blessed"] } | |
(all - i).times { ret << RATIO["normal"] } | |
return ret | |
end | |
# 引数チェック(適当) | |
# === Note | |
# 一応 [現在の重さ] [目的の重さ] と入力してもらうことにする | |
# interactive? なにそれおいしいの | |
def get_param | |
unless ARGV.length == 2 | |
puts "invalid argument" | |
puts "usage: ruby #{$0} [current_weight] [goal_weight]" | |
exit(1) | |
end | |
return ARGV[0].to_f, ARGV[1].to_f | |
end | |
# 何回羽の巻物をよみゃいいか | |
# === Note | |
# 計算基準としては通常の羽の巻物を読みまくって1.0s以下になった時までの回数 | |
# ほら、妖精が装備できるのが1.0sじゃん? (適当) | |
def get_loop_counter(w) | |
ret = 1 | |
loop do | |
t = w * (RATIO["normal"] ** ret) | |
return ret if t <= 1.0 | |
ret += 1 | |
end | |
end | |
# main 関数 | |
def main | |
weight, target = get_param() | |
t = get_loop_counter(weight) | |
comb = [] | |
# 検索組み合わせの初期化 | |
t.times do |i| | |
i.times do |j| | |
comb << add_ratio(i, j) | |
end | |
end | |
# sort: 重さが最も近く、かつ読む回数が少ないものを優先するため length で割っている | |
# 5個表示は適当 | |
comb.map{ |i| | |
w = i.reduce(:*) * weight | |
{ "weight" => w, "trial" => i } if w <= target | |
}.compact.sort!{ |a, b| | |
(-1) * (a["weight"] / a["trial"].length <=> b["weight"] / b["trial"].length) | |
}[0..4].each{ |i| i["trial"].map!{ |j| | |
case j | |
when RATIO["normal"] | |
"羽" | |
when RATIO["blessed"] | |
"祝福羽" | |
end | |
} | |
p i | |
} | |
end | |
if $0 == __FILE__ | |
main | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment