Created
March 2, 2016 20:17
-
-
Save JFFail/b1411a36072b71e8fef6 to your computer and use it in GitHub Desktop.
Solution To Reddit Daily Programmer #255 - Playing With Light
This file contains hidden or 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
#https://www.reddit.com/r/dailyprogrammer/comments/46zm8m/20160222_challenge_255_easy_playing_with_light/ | |
#Function for flipping the switches. | |
function FlipSwitch ($Array,$Position) | |
{ | |
$currentBoolean = $Array[$Position] | |
if($currentBoolean) | |
{ | |
$newValue = $false | |
} | |
else | |
{ | |
$newValue = $true | |
} | |
return $newValue | |
} | |
#First get input for the total number of lightswitches. | |
Write-Output "How many lightswitches are there?" | |
$numberOfSwitches = Read-Host "Enter a number" | |
#Create the array. | |
$lightSwitches = @() | |
#Set the initial values to all be off. | |
for($i = 0; $i -lt $numberOfSwitches; $i++) | |
{ | |
$lightSwitches += $false | |
} | |
#Loop through the input for calculating the lights. | |
$gettingInput = $true | |
while($gettingInput) | |
{ | |
$isValid = $true | |
do | |
{ | |
Write-Output "Enter the range, separated by a space. Enter 'n' to quit!" | |
$currentRange = Read-Host "Enter two numbers" | |
if($currentRange.ToLower() -eq "n") | |
{ | |
$gettingInput = $false | |
} | |
else | |
{ | |
$numberArray = $currentRange.Split(" ") | |
if($numberArray.Count -eq 2) | |
{ | |
#We have two numbers, make sure they're legit. | |
$firstNumber = [int]$numberArray[0] | |
$secondNumber = [int]$numberArray[1] | |
#Change the values if we need the criteria. | |
if(($firstNumber -ge 0) -and ($firstNumber -lt $numberOfSwitches) -and ($secondNumber -ge 0) -and ($secondNumber -lt $numberOfSwitches)) | |
{ | |
if($firstNumber -le $secondNumber) | |
{ | |
for($i = $firstNumber; $i -le $secondNumber; $i++) | |
{ | |
$lightSwitches[$i] = FlipSwitch -Array $lightSwitches -Position $i | |
} | |
} | |
else | |
{ | |
for($i = $firstNumber; $i -ge $secondNumber; $i--) | |
{ | |
$lightSwitches[$i] = FlipSwitch -Array $lightSwitches -Position $i | |
} | |
} | |
$isValid = $true | |
} | |
else | |
{ | |
Write-Host "The values were not valid!" -ForegroundColor Red | |
$isValid = $false | |
} | |
} | |
else | |
{ | |
Write-Host "That input is invalid!" -ForegroundColor Red | |
$isValid = $false | |
} | |
} | |
} while(-not $isValid) | |
} | |
#Write the final output. | |
foreach($switch in $lightSwitches) | |
{ | |
if($switch) | |
{ | |
Write-Host "X" -NoNewline | |
} | |
else | |
{ | |
Write-Host "." -NoNewline | |
} | |
} | |
Write-Host "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment