Warning: Spoilers ahead!nRecently, I enjoyed watching Squid Game non NetFlix.nThere is a particular episode where the players need to cross a glass bridge.nA related mathematical puzzle was posted onnFiveThirtyEight.nThe puzzlen|”’| |”’|n| |oooooooooooooooooo| |n| |oooooooooooooooooo| |n|”’| |”’|nnnThere is a bridge made of 18 pairs of glass squares.nThere are 16 Players. Their objective is crossing the bridge.nOne player at at time attempts to cross thenbridge by picking one glass square from the next pair and jumping ot it.nOn each pair, one square is tempered glass and can withstand a jump. nThe other is not. A player that lands on it will break it and die.nFind the average number of playersnthat make it to the other side.nnSimulate to solvennBeing a programmer, I find simulatingnthe solution more interesting than doing the math. So, here we go.nThis type of puzzle involves random variables and processesnand can be solved by running a simple Monte Carlo simulation.nIt’s not as fancy as it sounds! The basic premise is we define na single game run,nthen do this a bunch of times, collecting the number of alive players after each run. nFinally, we calculate the mean, median and mode of the data.nSome observations:nnThe bridge glass square configuration is random and varies in each game run.nIt follows that each jump is the equivalent of tossing a coin. In code, it looks like anrandom.randint(0, 1)ndying is basically guessing the wrong coin side. Assuming that 1 is the “bad glass”,nthen whenever the random generator returns a 1 the player dies.nwhatever the result, the rest of the players should be aware of it.nnHere is the entire Python code:n# Simulation of https://fivethirtyeight.com/features/can-you-survive-squid-game-riddler/nfrom random import randintnimport statisticsnntimes = 100000nndata = []n# bridge made up of 18 pairs of separated glass squares.nBRIDGE_SQUARES = 18nBAD_GLASS = 1nnfor i in range(times):n # In this round, you are one of the 16 remaining competitors.n alive_competitors = 16n known_squares = 0nn while known_squares < BRIDGE_SQUARES:n # everyone died!n if alive_competitors == 0:n breakn # choose one of the two squares in a pair to land on.n square_to_land = randint(0, 1)n if square_to_land == BAD_GLASS:n alive_competitors -= 1n # Once a pair is revealed all remaining competitors take notice.n known_squares += 1nn data.append(alive_competitors)nnmean = statistics.mean(data)nmedian = statistics.median(data)nmode = statistics.mode(data)nn# On average, how many of the competitors survive and make it to the nextn# round of the competition?nprint(f'alive competitors: mean: {mean} median: {median} mode: {mode}')nnnalso available as a gist

# Category: python

# Python3 unquote URL one-liner

python3 -c ‘from urllib.parse import unquote; print(unquote(input(“> enter URL to decode: “)))’

# Generate a secure token using python 3

python3 -c ‘import secrets; print(secrets.token_urlsafe(64))’

# WordPress.com API from Python: Private blog posts.

kountanis.com is a static site, generated by a Python script.nI like it that way. It’s flexibile, ages well, and experimentation is easy.nBut I want to add my instagram pics, tweets, and wordpress posts into it.

# Simple CircuitBreaker in Python

If an app is communicating with remote/networked softwaren(think databases, key-value stores,nwebservices and queues to name a few)nit can and will fail.

# Python greek numeral converter

Quick code for converting to Greek Numerals. Doesn’t handle Myriads.

# Visualizing Euclidean Rhythms in Python

Visualization can help explore the patterns of Euclidean rhythms. Here are some generated .png images using Python and pillow.

# Euclidean rhythms in Python

Recently I’ve been playing around with Euclidean rhythms.nExploring stacked polyrhythms and asymmetrical looping.nI tend to prefer it’s output when not using gcd-based numbers (the original paper used it to recreate middle-eastern/traditional rhythms).nThe Python implementation I am using