Skip to content

Instantly share code, notes, and snippets.

View teryror's full-sized avatar

Tristan Dannenberg teryror

View GitHub Profile
@teryror
teryror / bin.rs
Last active January 28, 2021 12:31
Revised land count analysis for Magic: The Gathering
use num_bigint::BigUint;
use num_traits::ToPrimitive;
// calculate the binomial coefficient `n choose k`
fn binomial_coefficient(n: u32, k: u32) -> u64 {
let mut result = BigUint::from(1u32);
for i in 0..k {
result = (result * (n - i)) / (i + 1);
}
result.to_u64().unwrap()
@teryror
teryror / mtg_arena_win_rates.rs
Last active April 4, 2021 10:04
Finding the minimum win rates required to "go infinite" in MTG Arena
fn binomial_coefficient(n: u32, k: u32) -> u128 {
let mut res = 1u128;
for i in 0..k {
res = (res * (n - i) as u128) / (i + 1) as u128;
}
res
}
fn neg_binom_pmf(k: u32, r: u32, p: f64) -> f64 {
assert!(r > 0);
@teryror
teryror / mtga_price_parity.rs
Created April 5, 2021 10:23
Finding the win rates required to achieve rare card price parity with the MTG Arena Store
fn binomial_coefficient(n: u32, k: u32) -> u128 {
let mut res = 1u128;
for i in 0..k {
res = (res * (n - i) as u128) / (i + 1) as u128;
}
res
}
fn neg_binom_pmf(k: u32, r: u32, p: f64) -> f64 {
assert!(r > 0);
// Calculate the area of a generalized ellipse (abs(x/a)^n + abs(y/b)^n = 1).
// I'm considering this as the metric to minimize for a weird optimization
// problem, where this would be evaluated _a lot_, so I tried making this
// as fast & simple as possible.
pub fn area_of_superellipse(exponent: f64, semi_axes: (f64, f64)) -> f64 {
let n = exponent;
let (a, b) = semi_axes;
// This method was derived from the following formula:
@teryror
teryror / constexpr_alias_method.rs
Last active August 22, 2021 09:24
Const evaluatable Rust implementation of Vose's Alias Method
/// Const evaluatable Rust implementation of Vose's Alias Method, as described
/// by Keith Schwarz at https://www.keithschwarz.com/darts-dice-coins/
///
/// In brief, this is an O(n) precomputation, which allows sampling an arbitrary
/// finite probability distribution in O(1) time, by first simulating a fair
/// n-sided die, followed by a biased coin.
///
/// Because floating point arithmetic cannot be used in const functions, this is
/// built to operate on integer weights, rather than precomputed probabilities.
///
@teryror
teryror / _tv-tropes-scraper.rs
Last active April 5, 2024 08:12
Scrape links off TV Tropes to find common tropes in a set of shows
use scraper::{Html, Selector};
use std::collections::HashMap;
fn main() {
let source_sites: &[&[&str]] = &[
&[
"https://tvtropes.org/pmwiki/pmwiki.php/BreakingBad/TropesAToB",
"https://tvtropes.org/pmwiki/pmwiki.php/BreakingBad/TropesCToD",
"https://tvtropes.org/pmwiki/pmwiki.php/BreakingBad/TropesEToL",