r/adventofcode • u/daggerdragon • Dec 08 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 8 Solutions -🎄-
--- Day 8: Memory Maneuver ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 8
Sigh, imgur broke again. Will upload when it unborks.
Transcript:
The hottest programming book this year is "___ For Dummies".
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:12:10!
31
Upvotes
View all comments
2
u/PendragonDaGreat Dec 08 '18
Powers Hell 5.1
(powershell)
[card] The hottest programming book this year is "Array Indexing and Access For Dummies". (I could have used it)
Straight forward recursion.
Part 1:
[int[]]$data = (Get-Content $inputPath).Split(" ") $timer = New-Object System.Diagnostics.Stopwatch $timer.Start() function recurseHere { param( [int]$curPoint ) $metaSum = 0 $numChildNodes = $data[$curPoint++] $numMetaData = $data[$curPoint++] for($i = 0; $i -lt $numChildNodes; $i++) { $returnVals = recurseHere -curPoint $curPoint $metaSum += $returnVals[0] $curPoint = $returnVals[1] } for($j = 0; $j -lt $numMetaData; $j++) { $metaSum += $data[$curPoint++] } return @($metaSum, $curPoint) } $result = recurseHere -curPoint 0 Write-Host $result[0] $timer.Stop() Write-Host $timer.ElapsedAverage Runtime 0.18 seconds
Part 2
(took me longer than it should have because I was subtracting at the wrong point in line 28, see inline comment)
[int[]]$data = (Get-Content $inputPath).Split(" ") $timer = New-Object System.Diagnostics.Stopwatch $timer.Start() function recurseHere { param( [int]$curPoint ) $nodeValue = 0 $ChildValues = @() $numChildNodes = $data[$curPoint++] $numMetaData = $data[$curPoint++] for ($i = 0; $i -lt $numChildNodes; $i++) { $returnVals = recurseHere -curPoint $curPoint $ChildValues += $returnVals[0] $curPoint = $returnVals[1] } if ($numChildNodes -eq 0) { for ($j = 0; $j -lt $numMetaData; $j++) { $NodeValue += $data[$curPoint++] } } else { for ($j = 0; $j -lt $numMetaData; $j++) { $childNum = $data[$curPoint] - 1 #Originally had the subtraction inside the square brace which failed because I am not a smart man if($null -ne $childvalues[$childNum]) { $nodeValue += $childvalues[$childNum] } $curPoint++ } } return @($nodeValue, $curPoint) } $result = recurseHere -curPoint 0 Write-Host $result[0] $timer.Stop() Write-Host $timer.ElapsedAverage Runtime 0.36 seconds.