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