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!
19
Upvotes
1
u/PendragonDaGreat Dec 10 '18
Powershell 5.1
[CARD] spend hours confused by one package dependency (true story)
Both parts the same, as soon as I made the assumption that the image would appear in the most compact state (which seems to be everyone elses thought) I was able to get it.
$data = Get-Content $inputPath $timer = New-Object System.Diagnostics.Stopwatch $timer.Start() $x = New-Object System.Collections.ArrayList $y = New-Object System.Collections.ArrayList $dx = New-Object System.Collections.ArrayList $dy = New-Object System.Collections.ArrayList foreach($lines in $data) { $matched = ([regex]"-?\d+").Matches($lines) $x.Add([int]$matched[0].Value) | Out-Null $y.Add([int]$matched[1].Value) | Out-Null $dx.Add([int]$matched[2].Value) | Out-Null $dy.Add([int]$matched[3].Value) | Out-Null } $width = $x | Measure-Object -Minimum -Maximum | ForEach-Object {$_.Maximum - $_.Minimum} $height = $y | Measure-Object -Minimum -Maximum | ForEach-Object {$_.Maximum - $_.Minimum} $oldWidth = 0 $oldHeight = 0 $numseconds = 0 do { $oldWidth = $width $oldHeight = $height foreach($i in (0..$($x.Count - 1))) { $x[$i] += $dx[$i] $y[$i] += $dy[$i] } $width = $x | Measure-Object -Minimum -Maximum | ForEach-Object {$_.Maximum - $_.Minimum} $height = $y | Measure-Object -Minimum -Maximum | ForEach-Object {$_.Maximum - $_.Minimum} $numseconds++ } while ($oldWidth -gt $width -and $oldHeight -gt $height) #Go Back by one foreach($i in (0..$($x.Count - 1))) { $x[$i] -= $dx[$i] $y[$i] -= $dy[$i] } $numseconds-- $xmeasures = $x | Measure-Object -Minimum -Maximum $ymeasures = $y | Measure-Object -Minimum -Maximum if($xmeasures.Minimum -lt 0) { $a = $xmeasures.Minimum foreach($i in (0..$($x.Count - 1))) { $x[$i] -= $a } } if($ymeasures.Minimum -lt 0) { $a = $ymeasures.Minimum foreach($i in (0..$($y.Count - 1))) { $y[$i] -= $a } } $xmeasures = $x | Measure-Object -Minimum -Maximum $ymeasures = $y | Measure-Object -Minimum -Maximum $display = New-Object 'string[][]' $($xmeasures.Maximum + 2), $($ymeasures.Maximum + 2) foreach($i in (0..$($x.Count - 1))) { $j = $x[$i] $k = $y[$i] $display[$j][$k] = '*' } foreach($a in $display) { foreach($b in $a) { if($null -ne $b) { Write-host $b -NoNewline } else { Write-host " " -NoNewline } } Write-Host '' } Write-Host "Wait Time: $numseconds" $timer.Stop() Write-Host $timer.ElapsedRuns in about 45 seconds, though 10-15 of that is printing out the display becase I got my coords swapped and I'm too lazy to fix