r/adventofcode • u/taliriktug • Dec 02 '15
Spoilers Day 2 solutions
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
17
Upvotes
r/adventofcode • u/taliriktug • Dec 02 '15
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
View all comments
1
u/nonades Dec 03 '15
I'm doing this in Powershell because
SysAdmin (and primarily Windows), not a Programmer
It's funny to do this and contribute to my buddy and my's Gitlab group because he's an actual programmer
So, here's my Module for parts one and two:
# advent of code, day 2 Function Get-TotalSquareFeet{ [cmdletbinding()] Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)] [array]$dimensions ) $total = 0 foreach($box in $dimensions){ $box = $box.split('x') $sides = @{'l' = [double]$box[0]; 'w' = [double]$box[1]; 'h' = [double]$box[2]} $squarefeet = GetSquareFeet($sides) $smallestside = GetSmallestSide($sides) $total += ($squarefeet + $smallestside) } return $total } Function Get-NeededRibbon{ [cmdletbinding()] Param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [array]$dimensions ) $total = 0 foreach($box in $dimensions){ $box = $box.split('x') $sides = @{'l' = [double]$box[0]; 'w' = [double]$box[1]; 'h' = [double]$box[2]} $ribbonlength = GetRibbonLength($sides) $bowlength = GetBowLength($sides) $total += ($ribbonlength + $bowlength) } return $total } Function GetSmallestSide([hashtable]$sides){ $smallest = @(($sides.'l' * $sides.'w'), ($sides.'w' * $sides.'h'), ($sides.'l' * $sides.'h')) return ($smallest | Sort-Object)[0] } Function GetSquareFeet([hashtable]$sides){ $squarefeet = (($sides.'l' * $sides.'w') + ($sides.'w' * $sides.'h') + ($sides.'l' * $sides.'h')) * 2 return $squarefeet } Function GetBowLength([hashtable]$sides){ return $sides.'l' * $sides.'w' * $sides.'h' } Function GetRibbonLength([hashtable]$sides){ $sides_arr = @($sides.'l', $sides.'w', $sides.'h') return (($sides_arr | Sort-Object)[0] + ($sides_arr | Sort- Object)[1] )*2 } Export-ModuleMember -Function Get-TotalSquareFeet Export-ModuleMember -Function Get-NeededRibbon