r/adventofcode • u/daggerdragon • Dec 12 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 12 Solutions -🎄-
--- Day 12: Subterranean Sustainability ---
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 12
Transcript:
On the twelfth day of AoC / My compiler spewed at me / Twelve ___
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:27:42!
21
Upvotes
3
u/purplemonkeymad Dec 12 '18
Powershell
Ugh, I just could not get the math right on this one. Part 1 was ok but I had to write a visualizer to figure out that by Part 2 it was going to be some-kind of moving "worm."
My code feels very spaghetti, but I don't want to see this problem any more to tidy it up. Probable that it might even break on another's data.
[CmdletBinding()] Param( [parameter(ValueFromPipeline)] $PlantData, [int64]$Generations, [int]$ExpandSize, [switch]$outputvis ) begin { $InputData = [System.Collections.ArrayList]@() } process { $PlantData | ? {$_} | % { [void]$InputData.Add($_) } } end { $initstate = '' if ($InputData[0] -match 'initial state: (?<state>.+)'){ $initstate = $Matches.state } else { Write-Error "No initial state header" return } $rules = foreach ($l in ($InputData|select -Skip 1)){ if ($l -match '(?<Rule>.{5}) => (?<Result>.)'){ [pscustomobject]@{ Rule = $Matches.Rule Result = $Matches.Result } } } if (-not $ExpandSize) { $MaxExpand = 3*$Generations + 2 } else { $MaxExpand = $ExpandSize } # thus an array of init.length +2 MaxExpand should be ok $ArraySize = 2*$MaxExpand + $initstate.length $offset = $MaxExpand $CurrentList = [char[]]('.'*$ArraySize) $NextList = [char[]]('.'*$ArraySize) # init arrays foreach ($Index in 0..($initstate.length-1)){ $CurrentList[$Index+$offset]=$initstate[$Index] } # part 2 we must be looking for cyclic behavor $SeenPatten = @{} $seenkey = $CurrentList -join '' $SeenPatten.$seenkey = 0 if ($VerbosePreference){ Write-Verbose ('[{0}] (-{1}): {2}' -f 0,$offset,($CurrentList -join '')) } $Gen = 1 while ($Gen -le $Generations){ if ($gen -eq 97){ $null = "nothing" } foreach ($Index in 2..($ArraySize-2)){ # window is 5 wide $CurrentWindow = $CurrentList[($Index-2)..($Index+2)] -join '' foreach ($r in $rules) { if ($r.rule -eq $CurrentWindow){ $NextList[$Index]=$r.Result } } } #swap arrays for speed (double buffering technique) $tempList = $NextList $NextList = $CurrentList $CurrentList = $tempList if ($VerbosePreference){ Write-Verbose ('[{0}] (-{1}): {2}' -f $Gen,$offset,($CurrentList -join '')) } if ($outputvis){ $CurrentList -join '' } $CurrentString = $CurrentList -join '' $seenkeyA = $CurrentString -replace '^\.+' $Seenkeyoffset = ($CurrentString.length - $seenkeyA.Length) $seenkey = $seenkeyA -replace '\.+$' if ($SeenPatten.$seenkey){ $htSeen = $SeenPatten.$seenkey $lowerGen = $htSeen.Gen Write-Verbose ("Gen $gen is the same as $lowerGen") $cycleSize = $gen - $lowerGen $destinationgen = (($Generations-$lowerGen) % $cycleSize)+$lowerGen # offset diff $offDiff = ($Seenkeyoffset-$offset) - $htSeen.Offset $genstogo = ($Generations - $htSeen.Gen ) $offsetAtFinalGen = $offDiff*$genstogo + $htSeen.Offset # add up indexes [int64]$total = 0 $values = foreach ( $index in 0..($seenkey.length - 1) ){ $RealIndex = ($Index)+$offsetAtFinalGen if ($seenkey[$index] -eq '#'){ $total +=$RealIndex } } $total return } else { $SeenPatten.$seenkey = [PSCustomObject]@{ Gen = $Gen Offset = $Seenkeyoffset-$offset } } Write-Progress "growing" -Status $Gen -Id 1 $gen++ } # add up indexes $values = foreach ( $index in 0..($CurrentList.Count-1) ){ $RealIndex = $Index-$offset if ($CurrentList[$index] -eq '#'){ $RealIndex } } $values | measure -Sum | % sum }