Skip to content

Instantly share code, notes, and snippets.

@mgritter
mgritter / compute.py
Created January 28, 2024 08:16
An analysis of dice mechanism by Elias Barry
outcomes = ["AS", "AF", "NS", "NF", "DS", "DF"]
# Game played with N-sided dice
# Player may reroll R times
# Success threshold is max of D dice.
def prob(N, R, D):
P = {}
EV = {}
advantage = range( 2 * N // 3 + 1, N+1 )
disadvantage = range( 1, N // 3 + 1 )
@mgritter
mgritter / draw.py
Last active September 25, 2023 03:43
Connected integer lattice points symmetric about the axes
from enumerate import visit_up_to_n
import matplotlib.pyplot as plt
import math
def draw( n ):
figures = []
def visitor( collection, cost ):
if cost == n:
figures.append( collection )
@mgritter
mgritter / janaka_problem.py
Created July 25, 2023 01:48
A combinatorial counting problem for lower-triangular sequences
# Each sequence consists of steps (0,0) (0,1) (1,0) or (1,1) as long as we
# don't hit the central diagonal. We are interested in seqeuences that end
# at (n-1,n) after 2n-1 steps.
def calculate(paths, max_n, min_k, max_k):
for k in range( min_k + 1, max_k + 1 ):
# TODO: limit y to only those reachable by a sequence of length <= k
for y in range( 1, max_n + 1 ):
# Skip diagonal and upper-triangulage entries, which are all zero
for x in range( 0, y ):
p1 = (x,y,k-1) # k'th element is (0,0)
@mgritter
mgritter / integers.dot
Created April 30, 2023 01:51
graphviz of 4-coloring integers of the form 2^x3^y when (a,2a,3a,4a) are all connected by edges
graph G {
# 0=red, 1=green, 2=blue, 3=gray
graph [nodesep="0.1", ranksep="0.1", pad="0.5"]
node [style=filled, fillcolor=white, shape=circle, width=0.4]
1 [fillcolor=red]
2 [fillcolor=green]
3 [fillcolor=gray]
4 [fillcolor=blue, fontcolor=white]
6 [fillcolor=red]
8 [fillcolor=gray]
@mgritter
mgritter / no_orthogonal.py
Created April 2, 2023 21:09
How many squares can be filled in the NxN grid such that no 5 orthogonal neighbors are all filled?
from ortools.sat.python import cp_model
import sys
# How many squares can be filled in the 10x10 grid such that no five
# orthogonally-connected squares are all filled?
m = cp_model.CpModel()
size = 10
@mgritter
mgritter / output.txt
Last active February 8, 2023 03:00
Which states have a word that uniquely identifies them? All other states have a letter shared with this word.
ALABAMA -- cover DHNOS optionally CEFGIJKPQRTUVWXYZ
ALASKA -- cover EHMNO optionally BCDFGIJPQRTUVWXYZ
ARIZONA -- cover DEGHLMSW optionally BCFJKPQTUVXY
CALIFORNIA -- cover DEGHMSWZ optionally BJKPQTUVXY
COLORADO -- cover BEHIKN optionally FGJMPQSTUVWXYZ
CONNECTICUT -- cover AGHKMS optionally BDFJLPQRVWXYZ
DELAWARE -- cover BHNOS optionally CFGIJKMPQTUVXYZ
FLORIDA -- cover BCEHNSW optionally GJKMPQTUVXYZ
GEORGIA -- cover HLNSW optionally BCDFJKMPQTUVXYZ
HAWAII -- cover LNOST optionally BCDEFGJKMPQRUVXYZ
@mgritter
mgritter / Repro.hs
Created December 21, 2022 18:56
LH file that causes Z3 to take forever
module Repro (part1) where
import qualified Data.Map as Map
import Data.List.Split
import Data.Either
import Data.Maybe
import Control.Monad.ST
import Data.STRef
{-@
@mgritter
mgritter / nonintersecting_lattice_paths.py
Created November 20, 2022 07:41
Counting pairs of non-edge-intersecting lattice paths.
def init_4d(n):
return [[[[0 for i in range(n)]
for j in range(n)]
for k in range(n)]
for l in range(n)]
def recurrence(c,x1,y1,x2,y2):
total = 0
# These two cases are always valid; even if the two paths have met at
# (x1,y1), then in these cases the previous step cannot be the same, so
@mgritter
mgritter / ant_paths.py
Created November 5, 2022 06:05
Counting pairs of nonintersecting or semi-intersection lattice paths
import cairo
import math
import itertools
def base_position( i, j ):
return (i * 60 + 10, j * 50 + 10)
def drawGrid( ctx, i, j ):
ctx.set_line_width( 1 )
ctx.set_source_rgba( 0.0, 0.0, 0.0, 1.0 )
@mgritter
mgritter / Factorial.fst
Last active November 10, 2021 18:01
An F* program for computing factorial sums
module Factorial
open FStar.Mul // Give us * for multiplication instead of pairs
open FStar.IO
open FStar.Printf
let rec factorial (n:nat) : nat =
if n = 0 then 1
else n * factorial (n-1)