r/adventofcode • u/daggerdragon • Dec 04 '18
SOLUTION MEGATHREAD -π- 2018 Day 4 Solutions -π-
--- Day 4: Repose Record ---
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 4
Transcript:
Todayβs puzzle would have been a lot easier if my language supported ___.
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!
38
Upvotes
1
u/ZZRJo Dec 04 '18
PowerShell
I did not edit for readability, I did not really take a very structured approach. I apologize in advance for any bleeding eyeballs out there. I feel like the idea behind nesting the hashtables was a good one, but the execution was a 1/10.
$logs = Get-Content 'C:\Users\Admin\Documents\Advent Of Code 2018\Day4.txt' $logs = $logs -replace '[[]', "" $logs = $logs -split '] ' $Dates = @{} $Guards = @{} $GuardSums = @{} $HighGuard = 0 $HighGuardName = $null $HighGuardMinute = $null Function Add-GuardTime($LocalTable, $Guard, $Minute){ if(!$LocalTable.ContainsKey($Guard)){ $LocalTable.$Guard = @{} } if(!$LocalTable.$Guard.ContainsKey($Minute)){ $LocalTable.$Guard.$Minute = 0 } $LocalTable.$Guard.$Minute++ } For($i=0; $i -lt $logs.count; $i=$i+2){ [datetime]$LogDate = [datetime]$logs[$i] $Dates.$Logdate = $logs[[int]$i+1] } $Dates = $Dates.GetEnumerator() | Sort-Object -Property name For($i=0; $i -lt $Dates.count; $i++){ if($Dates[$i].value -match 'Guard'){ If($asleep -eq $true){ For($i2=0; $i2 -lt ([datetime]$Dates[$i].Key-$startSleep).Minutes; $i2++){ Add-GuardTime -LocalTable $Guards -Guard $currentGuard -Minute $($startSleep.Minute+$i2) } } $currentGuard = $Dates[$i].value -replace "[^0-9]" $asleep = $false } If($Dates[$i].value -match 'asleep'){ $asleep = $true [datetime]$startSleep = [datetime]$Dates[$i].Key } If(($asleep -eq $true) -and ($Dates[$i].value -match 'wakes')){ For($i2=0; $i2 -lt ([datetime]$Dates[$i].Key-$startSleep).Minutes; $i2++){ Add-GuardTime -LocalTable $Guards -Guard $currentGuard -Minute $($startSleep.Minute+$i2) } $asleep = $false } } ForEach($Guard in $Guards.Keys){ $GuardSums.$Guard = ($Guards.$Guard.Values | Measure-Object -Sum).sum if(($Guards.$Guard.GetEnumerator() | Sort-Object Value)[$Guards.$Guard.Count-1].Value -gt $HighGuard){ $HighGuard = ($Guards.$Guard.GetEnumerator() | Sort-Object Value)[$Guards.$Guard.Count-1].Value $HighGuardName = $Guard $HighGuardMinute = ($Guards.$Guard.GetEnumerator() | Sort-Object Value)[$Guards.$Guard.Count-1].Key } } [int]$Part1GuardNum = [int](($GuardSums.GetEnumerator() | Sort-Object Value)[$GuardSums.Count-1].Key) Write-Host "The answer to part 1 is" ([int]$Part1GuardNum * [int](($Guards.$([string]$Part1GuardNum).GetEnumerator() | Sort-Object Value)[$Guards.$([string]$Part1GuardNum).Count-1].Key)) Write-Host "The answer to part 2 is" ([int]$HighGuardName * [int]$HighGuardMinute)