Skip to content

Instantly share code, notes, and snippets.

@kana
Created June 9, 2012 16:26
Show Gist options
  • Save kana/2901662 to your computer and use it in GitHub Desktop.
Save kana/2901662 to your computer and use it in GitHub Desktop.
即席ハトクラ手札シミュレーター
# coding: utf-8
ALL_CARDS = [
{id: '1', short_name: '農', name: '農村'},
{id: '2', short_name: '都', name: '都市'},
{id: '3', short_name: '大', name: '大都市'},
{id: 'x', short_name: 'ダ', name: '見習い侍女'},
]
CARD_FROM_ID_TABLE = Hash[ALL_CARDS.map {|c| [c[:id], c]}]
HAND_SIZE = 5
def normalize_hand!(card_ids)
card_ids.sort!()
end
def simulate(deck)
(0..deck.length-1).step(HAND_SIZE).
map {|i|
(i..i+HAND_SIZE-1).
select {|j| j < deck.length}.
map! {|j| deck[j]}
}.
map! {|hand|
normalize_hand!(hand)
}
end
def key_from_hands(hands)
hands.
map {|hand| hand.join('')}.
join('-')
end
def hands_from_key(key)
key.
split('-').
map {|ids_string| ids_string.chars}
end
def main()
if ARGV.length != 2
print "Usage: hatokura.rb N DECK_CODE\n"
print "\n"
print "N is a number of trials.\n"
print "DECK_CODE is a sequence of characters.\n"
print "The meaning of each charcter is as follows:\n"
print "\n"
ALL_CARDS.each do |c|
print " #{c[:id]} => (#{c[:short_name]}) #{c[:name]}\n"
end
exit 1
end
n = ARGV[0].to_i()
deck_code = ARGV[1]
deck = deck_code.chars.to_a()
handss =
(1..n).
map {|_|
simulate(deck.shuffle())
}
results = Hash.new(0) # hands => occurrence count
handss.each do |hands|
results[key_from_hands(hands)] += 1
end
maximum_occurrence = results.values.sort().last()
occurrence_width = maximum_occurrence.to_s.length
results.keys.sort().each do |key|
occurrence = results[key]
pp_hands =
hands_from_key(key).
map {|hand|
hand.map {|id| CARD_FROM_ID_TABLE[id][:short_name]}.join('-')
}.
join(' / ')
print "%*d (%5.2f%%): %s\n" % [
occurrence_width,
occurrence,
occurrence * 100.0 / n,
pp_hands
]
end
end
main()
------------------------------------------------------------
都市-都市スタートで:
3ターン目に6コイン以上が出る確率: 21.19%
4ターン目に6コイン以上が出る確率: 21.19%
------------------------------------------------------------
$ ruby hatokura.rb 1000000 1111111xxx22
1241 ( 0.12%): 2 - 5 - 4
12734 ( 1.27%): 2 - 6 - 3
12608 ( 1.26%): 2 - 7 - 2
6427 ( 0.64%): 3 - 4 - 4
55544 ( 5.55%): 3 - 5 - 3
63190 ( 6.32%): 3 - 6 - 2
24840 ( 2.48%): 3 - 7 - 1
6238 ( 0.62%): 4 - 3 - 4
75710 ( 7.57%): 4 - 4 - 3
129041 (12.90%): 4 - 5 - 2
75675 ( 7.57%): 4 - 6 - 1
6381 ( 0.64%): 4 - 7 - 0
1278 ( 0.13%): 5 - 2 - 4
55643 ( 5.56%): 5 - 3 - 3
129079 (12.91%): 5 - 4 - 2
115993 (11.60%): 5 - 5 - 1
16512 ( 1.65%): 5 - 6 - 0
12453 ( 1.25%): 6 - 2 - 3
62987 ( 6.30%): 6 - 3 - 2
76060 ( 7.61%): 6 - 4 - 1
16509 ( 1.65%): 6 - 5 - 0
12472 ( 1.25%): 7 - 2 - 2
25248 ( 2.52%): 7 - 3 - 1
6137 ( 0.61%): 7 - 4 - 0
------------------------------------------------------------
$ ruby hatokura.rb 1000000 1111111xxx
83521 ( 8.35%): 農-農-農-農-農 / 農-農-ダ-ダ-ダ
417076 (41.71%): 農-農-農-農-ダ / 農-農-農-ダ-ダ
416191 (41.62%): 農-農-農-ダ-ダ / 農-農-農-農-ダ
83212 ( 8.32%): 農-農-ダ-ダ-ダ / 農-農-農-農-農
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment