Warning: Spoilers ahead!

Recently, I enjoyed watching Squid Game on NetFlix. There is a particular episode where the players need to cross a glass bridge. A related mathematical puzzle was posted on FiveThirtyEight.

### The puzzle

```
|'''| |'''|
| |oooooooooooooooooo| |
| |oooooooooooooooooo| |
|'''| |'''|
```

- There is a bridge made of 18 pairs of glass squares.
- There are 16 Players. Their objective is crossing the bridge.
- One player at at time attempts to cross the bridge by picking one glass square from the next pair and jumping ot it.
- On each pair, one square is tempered glass and can withstand a jump. The other is not. A player that lands on it will break it and die.
- Find the average number of players that make it to the other side.

### Simulate to solve

Being a programmer, I find simulating the solution more interesting than doing the math. So, here we go.

This type of puzzle involves random variables and processes and can be solved by running a simple Monte Carlo simulation.

It's not as fancy as it sounds! The basic premise is we define a single game run, then do this a bunch of times, collecting the number of alive players after each run. Finally, we calculate the mean, median and mode of the data.

Some observations:

- The bridge glass square configuration is random and varies in each game run.
- It follows that each jump is the equivalent of tossing a coin. In code, it looks like a
`random.randint(0, 1)`

- dying is basically guessing the wrong coin side. Assuming that
`1`

is the "bad glass", then whenever the random generator returns a 1 the player dies. - whatever the result, the rest of the players should be aware of it.

Here is the entire Python code:

```
# Simulation of https://fivethirtyeight.com/features/can-you-survive-squid-game-riddler/
from random import randint
import statistics
times = 100000
data = []
# bridge made up of 18 pairs of separated glass squares.
BRIDGE_SQUARES = 18
BAD_GLASS = 1
for i in range(times):
# In this round, you are one of the 16 remaining competitors.
alive_competitors = 16
known_squares = 0
while known_squares < BRIDGE_SQUARES:
# everyone died!
if alive_competitors == 0:
break
# choose one of the two squares in a pair to land on.
square_to_land = randint(0, 1)
if square_to_land == BAD_GLASS:
alive_competitors -= 1
# Once a pair is revealed all remaining competitors take notice.
known_squares += 1
data.append(alive_competitors)
mean = statistics.mean(data)
median = statistics.median(data)
mode = statistics.mode(data)
# On average, how many of the competitors survive and make it to the next
# round of the competition?
print(f'alive competitors: mean: {mean} median: {median} mode: {mode}')
```

also available as a gist