#!/usr/bin/perl

#
# - Bus Problem Simulation
# - Accepts n busses, each with its own frequency (10 minutes, for example).
# - With each trial, calculates a random number for each bus in each specific
#   range, from 0 to frequency (0 to 10 minutes, for example).
# - Takes the smallest of the random numbers and adds it to a counter, which
#   at the end will represent the total time in wait.  Average is taken by
#   dividing by the number of trials, 10000.
#

print "How many busses are there? "; $n = <STDIN>; chomp($n);

#
# $i starts at one; there will be no bus zero.
#
for ($i = 1; $i <= $n; $i++) {
  print "Frequency of bus $i? "; $bus[$i] = <STDIN>; chomp($bus[$i]);
}

$t = 10000;

for ($i = 0; $i < $t; $i++) {       # Run t trials of the simulation
  $shortestWait = rand($bus[1]);	# Find the wait time for bus 1 to start
  for($j = 2; $j <= $n; $j++) {		# Cycle through busses 2 thru n
    $newWait = rand($bus[$j]);		# Find the wait time for bus j
    if($newWait < $shortestWait) {	# If new wait time is less than old time
      $shortestWait = $newWait;		#   then use the new time now.
    }
  }
  $c += $shortestWait;  			# Add the time to c
}

$p = $c / $t;
print "Average Wait Time: $p minutes\n";
