Skip to content

Instantly share code, notes, and snippets.

@iconjack
iconjack / snakesladders.py
Created February 6, 2020 00:36
Compute expected number of turns in Snakes & Ladders
import numpy as np
# Compute expected number of turns to complete a snakes & ladders
# track of length n, given a set of portals (snakes and ladders),
# which are both represented as lists of pairs (start, end).
# It's ok to pass lists of [start, end] instead of pairs (start, end).
def E(n, portals):
portal_map = {k: k for k in range(n)} # identity map
portal_map.update(portals)
from itertools import combinations
from string import ascii_uppercase
states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia",
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
; Inputs: A and B are bit-strings of length N,
; right-justified in EAX and EBX respectively;
; N ≤ 30 is in ECX, and A ≠ B.
;
; Output: ESI/EDI (a fraction) is the probability
; that bit-string A beats bit-string B.
Magic:
PUSH EAX
PUSH EBX
from itertools import permutations
# Program to solve IBM's Ponder This puzzle for January 2024.
# https://research.ibm.com/haifa/ponderthis/challenges/January2024.html
#
# Using each of the numbers 1–16 exactly once, fill in the blank squares
# in the grid below, so that the 16 equations are true.
#
# ┌───┬───┬───┬───┬───┬───┬───┐
# │ │ + │ │ − │ │ − │ │ 5