Created
October 28, 2024 22:55
-
-
Save 123andy/f7a63e5694e5e8df8983d223d46f63c6 to your computer and use it in GitHub Desktop.
Airplane boarding puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sample Output: | |
Airplane Boarding Problem | |
Ran simluation a total of 10000 times boarding 100 people per plane. | |
A total of 4970 of 10000 times (49.7%) the last passenger got their assigned seat. | |
The number of non-assigned passengers varied from 0 to 14 with an avg of 5.1828 | |
Winners: 4970 | |
The number of non-assigned passengers varied from 0 to 13 with an avg of 4.6796780684105 | |
Losers: 5030 | |
The number of non-assigned passengers varied from 2 to 14 with an avg of 5.6799204771372 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# https://medium.com/puzzle-sphere/can-you-solve-this-famous-interview-question-91d0db846935 | |
echo "<h1>Airplane Boarding Problem</h1>" . "<pre>"; | |
global $verbose; | |
// Set to true to print a lot more logging... | |
$verbose = false; | |
// This is the number of simulations you want to do | |
$sim_count = 10000; | |
// This is the number of passengers | |
$passenger_count = 100; | |
// A variable to hold results | |
$data = []; | |
// Loop through simulations | |
for ($i = 1; $i <= $sim_count; $i++) { | |
// $i = current loop | |
$result = []; | |
$result['simulation'] = $i; | |
$result['nonassigned_seat_count'] = 0; | |
// Build an empty seatmap where key is seat number the value is the passenger | |
$result['seat_map'] = array_fill(1,$passenger_count,0); | |
// Recursively board passengers | |
$result = boardPassenger(1, $result); | |
$data[$i] = $result; | |
} | |
echo "Ran simluation a total of $sim_count times boarding $passenger_count people per plane.\n\n"; | |
// Look at how many times it was successful | |
$successes = array_sum(array_column($data, 'success')); | |
echo "A total of $successes of $sim_count times (" . round($successes/$sim_count*100,3) . | |
"%) the last passenger got their assigned seat.\n"; | |
printNonAssignedSeatCountData($data); | |
$winners = array_filter($data, fn($r)=> $r['success'] == 1); | |
echo "Winners: " . count($winners) . "\n"; | |
printNonAssignedSeatCountData($winners); | |
$losers = array_filter($data, fn($r)=> $r['success'] == 0); | |
echo "Losers: " . count($losers) . "\n"; | |
printNonAssignedSeatCountData($losers); | |
function printNonAssignedSeatCountData($arr) { | |
$nasc = array_column($arr, 'nonassigned_seat_count'); | |
echo "The number of non-assigned passengers varied from " . min($nasc) . " to " . max($nasc) . | |
" with an avg of " . array_sum($nasc)/count($arr) . "\n\n"; | |
} | |
function logIt($msg) { | |
global $verbose; | |
if ($verbose) echo "$msg\n"; | |
} | |
function boardPassenger($passenger, $result) { | |
$seat_count = count($result['seat_map']); | |
$new_seat = $passenger; | |
// Is assigned seat available? (For all but passenger 1) | |
if ($result['seat_map'][$passenger] === 0 && $passenger !== 1) { | |
// Assign person to assigned seat | |
// $result['seat_map'][$passenger_number] = $passenger_number; | |
logIt("Passenger $passenger seated in assigned seat"); | |
} else { | |
// Randomly pick a seat for passenger | |
// Step 1: Get available seats (those with a value of 0) | |
$available_seats = array_filter($result['seat_map'], fn($k) => $k == 0); | |
logIt("There are " . count($available_seats) . " available seats"); | |
// Step 2: Pick randomly | |
$random_seat = array_rand($available_seats, 1); | |
logIt("Selected random seat $random_seat"); | |
$new_seat = $random_seat; | |
} | |
$result['seat_map'][$new_seat] = $passenger; | |
// Increment nonassigned_seat_count | |
if ($new_seat !== $passenger) { | |
$result['nonassigned_seat_count']++; | |
logIt("Unassigned count incremented"); | |
} | |
// See if we are done or if we need to continue | |
if ($passenger == $seat_count) { | |
// We are done - last passenger | |
logIt("We are on last passenger -- returning"); | |
// Did we win? | |
$result['success'] = ($passenger == $new_seat) ? 1 : 0; | |
// End recursive loop | |
return $result; | |
} else { | |
// Recurse to next passenger | |
logIt("Goto next passenger - just sat $passenger in $new_seat"); | |
return boardPassenger(++$passenger, $result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment