Skip to content

Instantly share code, notes, and snippets.

@wass88

wass88/cantstop.rs

Last active Jun 20, 2020
Embed
What would you like to do?
キャントストップ1発登頂チャンス cant stop oneshot probabilities
#![allow(dead_code)]
struct DiceRoll {
st: Vec<Vec<usize>>,
dices: usize,
}
fn dice_roll(dices: usize) -> DiceRoll {
return DiceRoll {
st: vec![vec![]],
dices: dices,
};
}
impl Iterator for DiceRoll {
type Item = Vec<usize>;
fn next(&mut self) -> Option<Vec<usize>> {
while let Some(t) = self.st.pop() {
if t.len() < self.dices {
for i in 1..7 {
let mut old = t.clone();
old.push(i);
self.st.push(old)
}
} else {
return Some(t);
}
}
return None;
}
}
struct AllTargets {
st: Vec<Vec<usize>>,
size: usize,
}
fn all_targets(size: usize) -> AllTargets {
return AllTargets {
st: vec![vec![1]],
size: size,
};
}
impl Iterator for AllTargets {
type Item = Vec<usize>;
fn next(&mut self) -> Option<Vec<usize>> {
while let Some(t) = self.st.pop() {
if t.len() - 1 < self.size {
for i in (t[t.len() - 1] + 1)..13 {
let mut old = t.clone();
old.push(i);
self.st.push(old)
}
} else {
return Some(t[1..self.size + 1].to_vec());
}
}
return None;
}
}
fn all_hips(dices: Vec<usize>) -> Vec<(usize, usize)> {
let sum = dices.iter().fold(0, |s, a| s + a);
let a = vec![
dices[0] + dices[1],
dices[1] + dices[2],
dices[2] + dices[0],
];
return a.iter().map(|&s| (s, sum - s)).collect();
}
fn succ_comb(dices: &Vec<usize>, targets: &Vec<usize>) -> bool {
for (x, y) in all_hips(dices.clone()) {
if targets.iter().any(|&i| i == x || i == y) {
return true;
}
}
return false;
}
fn fail_rate(dices: usize, targets: &Vec<usize>) -> f64 {
let mut sum = 0;
let mut failed = 0;
for t in dice_roll(dices) {
sum += 1;
if !succ_comb(&t, &targets) {
failed += 1;
}
}
return failed as f64 / (sum as f64);
}
fn all_fail_rate(dices: usize, targets: usize) -> Vec<(Vec<usize>, f64)> {
let mut res = vec![];
for t in all_targets(targets) {
let f = fail_rate(dices, &t);
res.push((t, f));
}
return res;
}
fn comp_succ(dices: usize, targets: &Vec<usize>, remains: &Vec<usize>) -> f64 {
// DP[0][1][2] -> rate
let mut dp = vec![vec! {vec!{0.0; remains[2]+1}; remains[1]+1}; remains[0] + 1];
dp[0][0][0] = 1.0;
for t0 in 0..remains[0] + 1 {
for t1 in 0..remains[1] + 1 {
for t2 in 0..remains[2] + 1 {
if t0 == 0 && t1 == 0 && t2 == 0 {
continue;
}
let mut cur = targets.clone();
if t0 == 0 {
cur[0] = 0
}
if t1 == 0 {
cur[1] = 0
}
if t2 == 0 {
cur[2] = 0
}
let mut sum = 0;
let mut nv = 0.0f64;
for d in dice_roll(dices) {
let mut rate = 0.0f64;
for (x, y) in all_hips(d.clone()) {
let ix = cur.iter().position(|&i| i == x);
let iy = cur.iter().position(|&i| i == y);
let c = |k| match k {
None => vec![0, 0, 0],
Some(0) => vec![1, 0, 0],
Some(1) => vec![0, 1, 0],
Some(2) => vec![0, 0, 1],
Some(_) => unreachable!(),
};
let cx = c(ix);
let cy = c(iy);
let c = vec![cx[0] + cy[0], cx[1] + cy[1], cx[2] + cy[2]];
let s = |a, b| ((a as i64 - b) as i64).max(0) as usize;
let r = dp[s(t0, c[0])][s(t1, c[1])][s(t2, c[2])];
rate = rate.max(r);
}
nv += rate;
sum += 1;
}
dp[t0][t1][t2] = nv as f64 / sum as f64;
}
}
}
return dp[remains[0]][remains[1]][remains[2]];
}
fn all_succ() {
let ms = vec![0, 0, 3, 5, 7, 9, 11, 13, 11, 9, 7, 5, 3];
for ts in all_targets(3) {
let m: Vec<usize> = ts.iter().map(|&s| ms[s]).collect();
let r = comp_succ(4, &ts, &m);
println!("{:?} {:?} => {}", ts, m, r)
}
}
fn playout(tries: usize, targets: &Vec<usize>, remains: &Vec<usize>) -> f64 {
let mut sum = 0;
let mut succ = 0;
for _ in 0..tries {
if playonce(targets, remains) {
succ += 1;
}
sum += 1;
}
return succ as f64 / sum as f64;
}
fn playonce(targets: &Vec<usize>, remains: &Vec<usize>) -> bool {
use rand::seq::SliceRandom;
use rand::Rng;
let mut rng = rand::thread_rng();
let mut remains: Vec<i64> = remains.iter().map(|&n| n as i64).collect();
loop {
let d = vec![
rng.gen_range(1, 7),
rng.gen_range(1, 7),
rng.gen_range(1, 7),
rng.gen_range(1, 7),
];
let mut ok = false;
let mut cur = targets.clone();
if remains[0] <= 0 {
cur[0] = 0
}
if remains[1] <= 0 {
cur[1] = 0
}
if remains[2] <= 0 {
cur[2] = 0
}
for (x, y) in all_hips(d.clone()) {
let ix = cur.iter().position(|&i| i == x);
let iy = cur.iter().position(|&i| i == y);
if ix == None && iy == None {
continue;
}
if let Some(ix) = ix {
remains[ix] -= 1
}
if let Some(iy) = iy {
remains[iy] -= 1
}
ok = true;
break;
}
if !ok {
return false;
}
if remains.iter().all(|&i| i <= 0) {
return true;
}
}
}
fn main() {
//println!("{:?}", dice_move(4, &vec!{2, 3, 5}));
all_succ();
//println!("{:?}", comp_succ(4, &vec! {6,7,8}, &vec! {2,2,2}));
//println!("{:?}", playout(100000, &vec! {6,7,8}, &vec! {2,2,2}));
//fail_rate(4, &vec!{6,7,8});
//let mut rates = all_fail_rate(4, 3);
//rates.sort_by(|c, d| c.1.partial_cmp(&d.1).unwrap());
//println!("{:?}", rates);
}
[2, 3, 4] [3, 5, 7] => 0.000025564439917238523
[2, 3, 5] [3, 5, 9] => 0.00005927363292716724
[2, 3, 6] [3, 5, 11] => 0.0003492213715781228
[2, 3, 7] [3, 5, 13] => 0.0012078961974730021
[2, 3, 8] [3, 5, 11] => 0.0011801101801356038
[2, 3, 9] [3, 5, 9] => 0.0005555811764881801
[2, 3, 10] [3, 5, 7] => 0.00019540987013117172
[2, 3, 11] [3, 5, 5] => 0.00004896451206860815
[2, 3, 12] [3, 5, 3] => 0.000023890874680418654
[2, 4, 5] [3, 7, 9] => 0.00020491958831998764
[2, 4, 6] [3, 7, 11] => 0.0014330734745359168
[2, 4, 7] [3, 7, 13] => 0.00368435603769096
[2, 4, 8] [3, 7, 11] => 0.0037016067910537395
[2, 4, 9] [3, 7, 9] => 0.0012210239234110823
[2, 4, 10] [3, 7, 7] => 0.0009434451208343333
[2, 4, 11] [3, 7, 5] => 0.00020972437016999315
[2, 4, 12] [3, 7, 3] => 0.00008801206947410924
[2, 5, 6] [3, 9, 11] => 0.0016962562883425507
[2, 5, 7] [3, 9, 13] => 0.003904996096209413
[2, 5, 8] [3, 9, 11] => 0.005045008379149678
[2, 5, 9] [3, 9, 9] => 0.0012841475752787043
[2, 5, 10] [3, 9, 7] => 0.0012310077072657751
[2, 5, 11] [3, 9, 5] => 0.0005993471314746929
[2, 5, 12] [3, 9, 3] => 0.00022515038009823107
[2, 6, 7] [3, 11, 13] => 0.013344623702580582
[2, 6, 8] [3, 11, 11] => 0.01595893560653182
[2, 6, 9] [3, 11, 9] => 0.0054833151535683385
[2, 6, 10] [3, 11, 7] => 0.003595726950184641
[2, 6, 11] [3, 11, 5] => 0.0013013575405945283
[2, 6, 12] [3, 11, 3] => 0.001053846095238263
[2, 7, 8] [3, 13, 11] => 0.02073492531747651
[2, 7, 9] [3, 13, 9] => 0.006220546534082804
[2, 7, 10] [3, 13, 7] => 0.00581978912998253
[2, 7, 11] [3, 13, 5] => 0.0020239523510390926
[2, 7, 12] [3, 13, 3] => 0.0021481134531276575
[2, 8, 9] [3, 11, 9] => 0.004153327241436465
[2, 8, 10] [3, 11, 7] => 0.003663851585755454
[2, 8, 11] [3, 11, 5] => 0.0009040918952828436
[2, 8, 12] [3, 11, 3] => 0.0010538460952382717
[2, 9, 10] [3, 9, 7] => 0.0005284985092256307
[2, 9, 11] [3, 9, 5] => 0.00016431921767766944
[2, 9, 12] [3, 9, 3] => 0.00022515038009823292
[2, 10, 11] [3, 7, 5] => 0.0000805487092437797
[2, 10, 12] [3, 7, 3] => 0.0000880120694741112
[2, 11, 12] [3, 5, 3] => 0.000023890874680418735
[3, 4, 5] [5, 7, 9] => 0.00016562394472824683
[3, 4, 6] [5, 7, 11] => 0.0008773157426008451
[3, 4, 7] [5, 7, 13] => 0.0024628283625393117
[3, 4, 8] [5, 7, 11] => 0.0025458986869387884
[3, 4, 9] [5, 7, 9] => 0.0015923944959876943
[3, 4, 10] [5, 7, 7] => 0.0010510706522522612
[3, 4, 11] [5, 7, 5] => 0.00021750782989781977
[3, 4, 12] [5, 7, 3] => 0.00008054870924377988
[3, 5, 6] [5, 9, 11] => 0.0013856375087786684
[3, 5, 7] [5, 9, 13] => 0.002178769424250058
[3, 5, 8] [5, 9, 11] => 0.003290067932883377
[3, 5, 9] [5, 9, 9] => 0.0015366847973816544
[3, 5, 10] [5, 9, 7] => 0.0011270203007502679
[3, 5, 11] [5, 9, 5] => 0.0004770496936408061
[3, 5, 12] [5, 9, 3] => 0.00016431921767766974
[3, 6, 7] [5, 11, 13] => 0.013083416029259857
[3, 6, 8] [5, 11, 11] => 0.009951723299418534
[3, 6, 9] [5, 11, 9] => 0.0050569479516541374
[3, 6, 10] [5, 11, 7] => 0.004299882462624441
[3, 6, 11] [5, 11, 5] => 0.0012754341757669808
[3, 6, 12] [5, 11, 3] => 0.0009040918952828353
[3, 7, 8] [5, 13, 11] => 0.023089935370854187
[3, 7, 9] [5, 13, 9] => 0.007512966609244167
[3, 7, 10] [5, 13, 7] => 0.006384508034410984
[3, 7, 11] [5, 13, 5] => 0.0019493193697614753
[3, 7, 12] [5, 13, 3] => 0.0020239523510390987
[3, 8, 9] [5, 11, 9] => 0.005802882626755753
[3, 8, 10] [5, 11, 7] => 0.0052977308866770725
[3, 8, 11] [5, 11, 5] => 0.0012754341757669986
[3, 8, 12] [5, 11, 3] => 0.0013013575405945407
[3, 9, 10] [5, 9, 7] => 0.0016027081654223525
[3, 9, 11] [5, 9, 5] => 0.00047704969364080116
[3, 9, 12] [5, 9, 3] => 0.0005993471314746975
[3, 10, 11] [5, 7, 5] => 0.00021750782989781608
[3, 10, 12] [5, 7, 3] => 0.00020972437016999242
[3, 11, 12] [5, 5, 3] => 0.00004896451206860658
[4, 5, 6] [7, 9, 11] => 0.002572182187115604
[4, 5, 7] [7, 9, 13] => 0.008419152469681558
[4, 5, 8] [7, 9, 11] => 0.007808721932728033
[4, 5, 9] [7, 9, 9] => 0.0026128589801566494
[4, 5, 10] [7, 9, 7] => 0.0038982399044457015
[4, 5, 11] [7, 9, 5] => 0.001602708165422363
[4, 5, 12] [7, 9, 3] => 0.0005284985092256341
[4, 6, 7] [7, 11, 13] => 0.02376029812820459
[4, 6, 8] [7, 11, 11] => 0.03482147108433543
[4, 6, 9] [7, 11, 9] => 0.011957962995389624
[4, 6, 10] [7, 11, 7] => 0.01549556825974864
[4, 6, 11] [7, 11, 5] => 0.00529773088667707
[4, 6, 12] [7, 11, 3] => 0.003663851585755429
[4, 7, 8] [7, 13, 11] => 0.03418466641667157
[4, 7, 9] [7, 13, 9] => 0.022045325734773642
[4, 7, 10] [7, 13, 7] => 0.0158805379200486
[4, 7, 11] [7, 13, 5] => 0.006384508034410957
[4, 7, 12] [7, 13, 3] => 0.005819789129982582
[4, 8, 9] [7, 11, 9] => 0.011089383300200801
[4, 8, 10] [7, 11, 7] => 0.015495568259748523
[4, 8, 11] [7, 11, 5] => 0.00429988246262429
[4, 8, 12] [7, 11, 3] => 0.0035957269501848102
[4, 9, 10] [7, 9, 7] => 0.0038982399044456754
[4, 9, 11] [7, 9, 5] => 0.0011270203007502312
[4, 9, 12] [7, 9, 3] => 0.0012310077072657298
[4, 10, 11] [7, 7, 5] => 0.0010510706522522538
[4, 10, 12] [7, 7, 3] => 0.000943445120834327
[4, 11, 12] [7, 5, 3] => 0.00019540987013117277
[5, 6, 7] [9, 11, 13] => 0.02316894550327935
[5, 6, 8] [9, 11, 11] => 0.02724093465836428
[5, 6, 9] [9, 11, 9] => 0.012242392243214485
[5, 6, 10] [9, 11, 7] => 0.01108938330020087
[5, 6, 11] [9, 11, 5] => 0.005802882626755763
[5, 6, 12] [9, 11, 3] => 0.00415332724143643
[5, 7, 8] [9, 13, 11] => 0.04481735379193718
[5, 7, 9] [9, 13, 9] => 0.009863523031459806
[5, 7, 10] [9, 13, 7] => 0.022045325734773847
[5, 7, 11] [9, 13, 5] => 0.007512966609244104
[5, 7, 12] [9, 13, 3] => 0.006220546534082767
[5, 8, 9] [9, 11, 9] => 0.012242392243214406
[5, 8, 10] [9, 11, 7] => 0.011957962995389586
[5, 8, 11] [9, 11, 5] => 0.005056947951653804
[5, 8, 12] [9, 11, 3] => 0.005483315153568336
[5, 9, 10] [9, 9, 7] => 0.00261285898015666
[5, 9, 11] [9, 9, 5] => 0.0015366847973816604
[5, 9, 12] [9, 9, 3] => 0.0012841475752786665
[5, 10, 11] [9, 7, 5] => 0.0015923944959876717
[5, 10, 12] [9, 7, 3] => 0.0012210239234110832
[5, 11, 12] [9, 5, 3] => 0.0005555811764881666
[6, 7, 8] [11, 13, 11] => 0.057548888319900036
[6, 7, 9] [11, 13, 9] => 0.044817353791937325
[6, 7, 10] [11, 13, 7] => 0.03418466641667139
[6, 7, 11] [11, 13, 5] => 0.02308993537085435
[6, 7, 12] [11, 13, 3] => 0.020734925317476702
[6, 8, 9] [11, 11, 9] => 0.027240934658364517
[6, 8, 10] [11, 11, 7] => 0.03482147108433481
[6, 8, 11] [11, 11, 5] => 0.009951723299418562
[6, 8, 12] [11, 11, 3] => 0.015958935606531915
[6, 9, 10] [11, 9, 7] => 0.0078087219327280305
[6, 9, 11] [11, 9, 5] => 0.003290067932883336
[6, 9, 12] [11, 9, 3] => 0.005045008379149739
[6, 10, 11] [11, 7, 5] => 0.002545898686938808
[6, 10, 12] [11, 7, 3] => 0.003701606791053711
[6, 11, 12] [11, 5, 3] => 0.001180110180135599
[7, 8, 9] [13, 11, 9] => 0.023168945503279262
[7, 8, 10] [13, 11, 7] => 0.023760298128204432
[7, 8, 11] [13, 11, 5] => 0.013083416029259588
[7, 8, 12] [13, 11, 3] => 0.013344623702580615
[7, 9, 10] [13, 9, 7] => 0.008419152469681534
[7, 9, 11] [13, 9, 5] => 0.0021787694242500107
[7, 9, 12] [13, 9, 3] => 0.003904996096209468
[7, 10, 11] [13, 7, 5] => 0.0024628283625393547
[7, 10, 12] [13, 7, 3] => 0.003684356037691129
[7, 11, 12] [13, 5, 3] => 0.0012078961974729672
[8, 9, 10] [11, 9, 7] => 0.0025721821871155385
[8, 9, 11] [11, 9, 5] => 0.0013856375087786834
[8, 9, 12] [11, 9, 3] => 0.0016962562883425644
[8, 10, 11] [11, 7, 5] => 0.0008773157426008375
[8, 10, 12] [11, 7, 3] => 0.0014330734745359405
[8, 11, 12] [11, 5, 3] => 0.00034922137157813223
[9, 10, 11] [9, 7, 5] => 0.00016562394472824425
[9, 10, 12] [9, 7, 3] => 0.00020491958831998867
[9, 11, 12] [9, 5, 3] => 0.00005927363292716705
[10, 11, 12] [7, 5, 3] => 0.000025564439917238984
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.