Simulating the squid game glass bridge puzzle

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 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

Simple TDD with NeoVim’s Terminal

Gotta love NeoVim’s terminal! For simple projects, it can replace a lot of whatndedicated plugins do, not to mention external dependencies like file watchers.n![kvdev]({{ site.baseurl }}/assets/img/kv.png)nHere is a TDD recipe for a small project. No plugins used:nStart nvimnnvim feature.pynnSplit a new terminal verticallyn:vsp | :terminalnnGet the terminal’s job id.nn:echo b:terminal_job_idn3nnSwitch to the other windownnwnnAdd an autocmd that sends a command to the terminal to run the tests. The job id is used here.n:au BufWritePost :call jobsend(3, “\clear\python3\“)n# do tddnnClean up the buffer’s autocmd. Run on the code window.n:au! *