Skip to content

Instantly share code, notes, and snippets.

@JFFail
Created October 12, 2015 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFFail/cc61708646452da27584 to your computer and use it in GitHub Desktop.
Save JFFail/cc61708646452da27584 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer #236 Solution
#Reddit Daily Programmer 236.
#https://www.reddit.com/r/dailyprogrammer/comments/3ofsyb/20151012_challenge_236_easy_random_bag_system/
#Initialize the array with all pieces.
$pieces = New-Object System.Collections.ArrayList
$counter = 0
$result = ""
#Pick the pieces.
while($counter -lt 50) {
#Fill the initial "bucket" with pieces.
$pieces.Add("O") | Out-Null
$pieces.Add("I") | Out-Null
$pieces.Add("S") | Out-Null
$pieces.Add("Z") | Out-Null
$pieces.Add("L") | Out-Null
$pieces.Add("J") | Out-Null
$pieces.Add("T") | Out-Null
#Loop through the letters until none are left!
while($pieces.Count -gt 0) {
#Pick a random number for the next piece.
if($pieces.Count -gt 1) {
$randomNum = Get-Random -Minimum 0 -Maximum ($pieces.Count - 1)
} else {
#Min and Max can't be the same value (0) so this is needed to finish off the bucket.
$randomNum = 0
}
#Append to the results.
$result += $pieces[$randomNum]
#Remove that item from the array.
$pieces.RemoveAt($randomNum)
#Increment the counter.
$counter++
#Break out of this IF we surpass the specified limit of pieces to generate.
if($counter -ge 50) {
break
}
}
}
#Output the results.
Write-Host $result
#Verify the output! Note the prompt specifies we always produce 50 pieces.
#This assumes the number could be variable to be a bit more resilient.
$resultArray = $result.ToCharArray()
$overCount = 0
$checkArray = New-Object System.Collections.ArrayList
$broken = $false
foreach($item in $resultArray) {
#Add to the check array and increment the counter.
$checkArray.Add($item) | Out-Null
$overCount++
<#Intentionally break stuff for testing. To use this, comment out the .Add on line 58.
if($overCount -lt 3) {
$checkArray.Add("J") | Out-Null
} else {
$checkArray.Add($item) | Out-Null
}#>
#Check each grouping of 7 characters OR if we're at the end check whatever is left.
if((($overCount % 7) -eq 0) -or ($overCount -eq $resultArray.Count)) {
#Put all the stuff into another list we can modify while enumerating through the original.
$fubarArray = New-Object System.Collections.ArrayList
foreach($item in $checkArray) {
$fubarArray.Add($item) | Out-Null
}
#Now check the "main" array.
foreach($item in $checkArray) {
#Check just one level deep. If it's found twice, script's broken. Don't care about more brokenness beyond that.
$index = $fubarArray.IndexOf($item)
$fubarArray.RemoveAt($index)
#Should always be -1 if we aren't borked. Kill it right away if it's not.
$index = $fubarArray.IndexOf($item)
if($index -ne -1) {
$broken = $true
break
}
}
#Clear the arraylist for the next run so we don't build on top of it.
$checkArray.Clear()
}
#Break again if we're busted since this loop will no longer matter.
if($broken) {
break
}
}
#Print if everything is valid or not.
if($broken) {
Write-Host "Output is not valid!"
} else {
Write-Host "Output is valid!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment