#!/usr/bin/perl

$totaltime = 0;

for($i = 0; $i < 10000; $i++) {
  $room = 3;
  $time = 0;

  until($room == 1) {
    $x = rand(1);
    if ($room == 3 and $x < 1/4) {$newroom = 2;}
    if ($room == 3 and $x > 1/4) {$newroom = 3;}
    if ($room == 2 and $x < 1/3) {$newroom = 1;}
    if ($room == 2 and $x > 1/3 and $x < 2/3) {$newroom = 2;}
    if ($room == 2 and $x > 2/3) {$newroom = 3;}
    $time++;
    $room = $newroom;
  }
  $totaltime += $time

}

$avgtime = $totaltime / 10000;

print "Average Time: $avgtime\n";
