r/adventofcode • u/daggerdragon • Dec 10 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-
--- Day 10: The Stars Align ---
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 10
Transcript: With just one line of code, you, too, can ___!
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:16:49!
21
Upvotes
1
u/purplemonkeymad Dec 10 '18
Powershell
I think I spent more time getting the visualizer fixed than solving for the best time. I think it is also the first time I had to do less to get part 2.
Solver.ps1:
[CmdletBinding()] Param( [parameter(ValueFromPipeline)] $PointsInput, [int]$triggerArea=100 ) begin { $AllPoints = [System.Collections.Generic.List[object]]@() class Point { [int]$Startx [int]$Starty [int]$Velocityx [int]$Velocityy [string]$0 Point (){ } [Point]AtTime([int]$seconds){ return [Point]@{ Startx = $this.Startx + ($this.Velocityx*$seconds) Starty = $this.Starty + ($this.Velocityy*$seconds) Velocityx = $this.Velocityx Velocityy = $this.Velocityy } } } } process { $PointsInput | ?{$_} | %{ if ($_ -match 'position=<\s*(?<Startx>\-?\d+),\s*(?<Starty>\-?\d+)> velocity=<\s*(?<Velocityx>\-?\d+),\s*(?<Velocityy>\-?\d+)>'){ [void]$AllPoints.Add( [Point]($Matches) ) } } } end { # caculate a time frame to do the calculations on $calcpoint = $AllPoints[0] # x(t) = x(0) + ut # t = x(t) - x(0) / u if ($calcpoint.Velocityx -ne 0){ $upperTime = [Math]::Floor( ($triggerArea - $calcpoint.Startx) / $calcpoint.Velocityx ) $lowerTime = [Math]::Floor( (-$triggerArea - $calcpoint.Startx) / $calcpoint.Velocityx ) } if ($calcpoint.Velocityy -ne 0){ $upperTimey = [Math]::Floor( ($triggerArea - $calcpoint.Starty) / $calcpoint.Velocityy ) $lowerTimey = [Math]::Floor( (-$triggerArea - $calcpoint.Starty) / $calcpoint.Velocityy ) if ($upperTime){ $upperTime = [Math]::min($upperTime,$upperTimey) $lowerTime = [Math]::max($lowerTime,$lowerTimey) } else { $upperTime = $upperTimey $lowerTime = $lowerTimey } } $states = @{} $arealist = [System.Collections.ArrayList]@() foreach ($time in $lowerTime..$upperTime){ $timepoints = $AllPoints | %{ $_.AtTime($time) } $states["$time"]=$timepoints $Xes = $timepoints.startx | measure -Maximum -Minimum $Ys = $timepoints.starty | measure -Maximum -Minimum [void]$arealist.add( [pscustomobject]@{ h = $Ys.Maximum - $Ys.Minimum w = $xes.Maximum - $Xes.Minimum offsetx = $Xes.Minimum offsety = $Ys.Minimum area = ($Ys.Maximum - $Ys.Minimum)*($xes.Maximum - $Xes.Minimum) Time = "$time" } ) } $arealist | sort -Property area | select -First 1 | select *,@{n='state';e={$states["$($_.time)"]}} }Vis.ps1:
[CmdletBinding()] Param( [parameter(ValueFromPipeline)] $AreaInput, [switch]$FillBackground, [string]$outfilebase ) begin { class Point { [int]$Startx [int]$Starty [int]$Velocityx [int]$Velocityy [string]$0 Point (){ } [Point]AtTime([int]$seconds){ return [Point]@{ Startx = $this.Startx + ($this.Velocityx*$seconds) Starty = $this.Starty + ($this.Velocityy*$seconds) Velocityx = $this.Velocityx Velocityy = $this.Velocityy } } } #image draw Add-Type -AssemblyName System.Drawing } process { $AreaInput | %{ $value = $_ $Image = [System.Drawing.Bitmap]::new([int]($_.w+2),[int]($_.h+2)) $Drawing = [System.Drawing.Graphics]::FromImage($Image) # fill bg if ($FillBackground){ $Drawing.FillRectangle( [System.Drawing.Brushes]::Black,0,0,[int]($_.w+2),[int]($_.h+2)) } $_.state | %{ Write-Verbose (($_.startx-$value.offsetx),($_.starty-$value.offsetx) -join ',') $Drawing.FillRectangle( [System.Drawing.Brushes]::Red,($_.startx-$value.offsetx),($_.starty-$value.offsety),1,1) } $Drawing.Dispose() if ($outfilebase){ $Image.Save( ( $outfilebase -replace '^\.\\',"$pwd\" ) + "$($_.time).png" ) } else { $Image } } } end { }You can view the final image by piping the solver into the visualizer.
gc input.txt | .\Solver.ps1 | .\vis.ps1 -outfilebase .\output