Created
June 3, 2015 21:12
-
-
Save JFFail/7468527e747a6bce4171 to your computer and use it in GitHub Desktop.
Solution To Reddit Daily Programmer #217
This file contains 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
#Reddit Daily Programmer #217 - http://www.reddit.com/r/dailyprogrammer/comments/3840rp/20150601_challenge_217_easy_lumberjack_pile/ | |
#Define the parameters of the piles. | |
<# | |
$grid = 3 | |
$logs = 7 | |
$stacks = @(@(1, 1, 1), @(2, 1, 3), @(1, 4, 1)) | |
#> | |
<# | |
$grid = 4 | |
$logs = 200 | |
$stacks = @(@(15, 12, 13, 11), @(19, 14, 8, 18), @(13, 14, 17, 15), @(7, 14, 20, 7)) | |
#> | |
$grid = 3 | |
$logs = 7 | |
$stacks = @() | |
#Get user input for the values. | |
$rounds = 0 | |
while($rounds -lt $grid) | |
{ | |
#Get the data from the user. | |
$pretty = $rounds + 1 | |
$userNums = Read-Host "Enter row $pretty" | |
#Place it into the array. | |
$stacks += , $userNums.Split(" ") | |
#Increment how many more times we need user input. | |
$rounds++ | |
} | |
#To get going, set the "lowest" value equal to the first element. | |
$lowest = [int]$stacks[0][0] | |
#Find the smallest value. | |
foreach($line in $stacks) | |
{ | |
#Get the smallest element in that line. | |
$smallest = $line | measure -Minimum | |
#See if it's smaller than what we already know. | |
if($smallest.Minimum -lt $lowest) | |
{ | |
$lowest = $smallest.Minimum | |
} | |
} | |
#Loop through the stacks, adding as necessary! | |
while($logs -gt 0) | |
{ | |
#Go through each of the rows. | |
for($i = 0; $i -lt $grid; $i++) | |
{ | |
#Go through each pile in that row. | |
for($j = 0; $j -lt $grid; $j++) | |
{ | |
#See if we need to add to it... and if we can. | |
if(([int]$stacks[$i][$j] -eq $lowest) -and ($logs -gt 0)) | |
{ | |
#Increment the pile and decrease our current log count. | |
[int]$stacks[$i][$j] += 1 | |
$logs -= 1 | |
} | |
} | |
} | |
#If we make it through once, increment the value of the lowest pile. | |
$lowest++ | |
} | |
#Print the stacks. | |
foreach($line in $stacks) | |
{ | |
Write-Host $line | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment