Skip to content

Instantly share code, notes, and snippets.

Avatar

Paul Hankin paulhankin

View GitHub Profile
@paulhankin
paulhankin / strat.txt
Last active Sep 3, 2021
Optimal strategy for 8-card game
View strat.txt
/ - / : WINS: none | LOSSES: none | DRAWS: 1, 2, 3, 4, 5, 6, 7, 8
/ - 1 : WINS: none | LOSSES: none | DRAWS: 2, 3, 4, 5, 6, 7, 8
/ - 2 : WINS: none | LOSSES: 1, 3, 4 | DRAWS: 5, 6, 7, 8
/ - 3 : WINS: none | LOSSES: 1, 2, 6 | DRAWS: 4, 5, 7, 8
/ - 4 : WINS: none | LOSSES: 1, 2, 7 | DRAWS: 3, 5, 6, 8
/ - 5 : WINS: none | LOSSES: 1, 3, 7 | DRAWS: 2, 4, 6, 8
/ - 6 : WINS: none | LOSSES: 1, 2, 3, 4, 7, 8 | DRAWS: 5
/ - 7 : WINS: none | LOSSES: 1, 4, 8 | DRAWS: 2, 3, 5, 6
/ - 8 : WINS: none | LOSSES: 1, 2, 3, 4, 6, 7 | DRAWS: 5
1 - 2 : WINS: none | LOSSES: 3 | DRAWS: 4, 5, 6, 7, 8
View guillotine.py
def g0(sx, sy, px, py, cache):
if sx < min(px, py) or sy < min(px, py):
return 0
if sx*sy < px*py:
return 0
if sx*sy == px*py:
return 1 if (sx==px and sy==py) or (sx==py and sy==px) else 0
best = 0
for i in range(1, sx):
best = max(best, guillotine(i, sy, px, py, cache) + guillotine(sx-i, sy, px, py, cache))
View a_test.go
package main
import (
"fmt"
"testing"
"github.com/db47h/rand64/xoroshiro"
)
const N = 10_000_000
View a.c
#include <stdio.h>
int main() {
int result = 1;
int n = 17;
for (int i=0; i < (2^64); i++) {
result = (result * n) % 115763;
}
printf("%d\n", result);
return 0;
@paulhankin
paulhankin / triangle.c.diff
Created Dec 22, 2018
patch for triangle.c to make it 64bit clean
View triangle.c.diff
diff --git a/triangle.c b/triangle.c
index f9e23fa..06cba71 100644
--- a/triangle.c
+++ b/triangle.c
@@ -342,11 +342,8 @@
#define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
-
-#include <inttypes.h>
View concat.py
def concat_optimal(xs, i=0, j=None, cache=None):
if cache is None:
i, j, cache = 0, len(xs), dict()
if (i, j) not in cache:
cache[i, j] = concat_optimal0(xs, i, j, cache)
return cache[i, j]
def concat_optimal0(xs, i, j, cache):
if j == i+1:
return 0, xs[i], i
View prime_pairs.py
def primes(n):
x = [False] * (n + 1)
for i in xrange(2, n + 1):
if x[i]: continue
yield i
for j in xrange(2 * i, n + 1, i):
x[j] = True
def prime_pairs(n):
ps = list(primes(n))
@paulhankin
paulhankin / poker.py
Created Dec 30, 2016
Compute optimal strategy for simple poker game
View poker.py
# Player A and B each get a number from 0 to n-1.
# Player A can:
# check: -1, 0, +1
# raise: B fold then +1
# : B call then -2, 0, +2
# strat[i] is probability that A raises with hand i.
# Then counterB(n, strat) gives B's optimal counter strategy.
def counterB(strat):
n = len(strat)
result = []
View test_sergio.py
import itertools
import random
def power_two(x):
return x and (x & (x - 1) == 0)
def asort(A, i, j):
assert power_two(j - i + 1), '%d - %d + 1 not power of 2' % (j, i)
if A[i-1] > A[j-1]:
A[i-1], A[j-1] = A[j-1], A[i-1]
@paulhankin
paulhankin / gist:2a05abd22be59bfb8268
Last active Aug 29, 2015
Full closed-hand Chinese poker data
View gist:2a05abd22be59bfb8268
front
0.11 : 4-3-2
0.19 : 5-3-2
0.24 : 5-4-2
0.30 : 5-4-3
0.41 : 6-3-2
0.48 : 6-4-2
0.56 : 6-4-3
0.61 : 6-5-2
0.69 : 6-5-3