<?php
/*
Challenge #1 from http://www.boylstontech.com/challenges.html
A room contains a row of 100 light bulbs. Below each light bulb is a switch to control the bulb above. To begin, each bulb is off. As a bright idea Solution Engineer, 
you decide to pass through the row, flipping each light on, one-by-one. 
Your next bright idea is to make a second pass, this time flipping the switch for every 2nd light (#2, #4, #6…..#100), turning half of the lights off. 
You make a third pass toggling the switch for every 3rd light, then another pass toggling every 4th, then 5th, then 6th, all the way up to your 100th pass when you change the setting of only the 100th light.  
Your simulation should accept an iteration number (1-100) and output the state of all light bulbs for that iteration. 

Solution By Ross Markham
*/

$passes = 0; // Default passes value
$LightsOn = 0;
$error = "";
$LightsBool = array();

for ($i = 1; $i <= 100; $i++) {
	$LightsBool[$i] = 0; // All lights start off
}

if(isset($_GET['passes'])){
   if(($_GET['passes'] < 1)||($_GET['passes'] > 100)){
      $error = "your iterations must be between 1 - 100";
   }else{
      $passes = $_GET['passes']; // Set number of iterations
   }
}

for ($i = 1; $i <= $passes; $i++) {      // How many passes to do
   for ($j = $i; $j <= 100; $j += $i){   // Flip switch every x Lights
      if($LightsBool[$j] == 0){
	     $LightsBool[$j] = 1; 
	  } else if ($LightsBool[$j] == 1){
	     $LightsBool[$j] = 0; 
	  } 
   }
}

if($error <> ""){ echo $error."<br /><br />"; }
echo "number of passes = ".$passes."<br />";
echo "Lights on and off in order - ";
for ($i = 1; $i <= 100; $i++) {
   echo $LightsBool[$i];
   if($LightsBool[$i] == 1){ $LightsOn++; }
}
echo "<br /> Lights on: ".$LightsOn." Lights off: ".(100 - $LightsOn)."<br /><br />";
$page = "http://".$_SERVER["SERVER_NAME"]."/".substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
echo "Try another iteration By going to :".$page."/?passes=&lt;no&gt;<br />";

?>