r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

41 Upvotes

426 comments sorted by

View all comments

Show parent comment

1

u/kibje Dec 03 '18 edited Dec 03 '18

I did something similar with hashtables. Please note that the nice names for the matches are not necessary at all, but I wrote them to keep my sanity.

$a = Get-Content ".\day_03.txt"
$timer = New-Object System.Diagnostics.Stopwatch
$timer.Start()
$pixels = New-Object System.Collections.generic.HashSet[String]
$dupes = New-Object System.Collections.generic.HashSet[String]
for ($i = 0; $i -lt $a.Count; $i++) {
    $b = $a[$i] -match '#([\d]+) @ ([\d]+),([\d]+): ([\d]+)x([\d]+)'
    [int]$id = $matches[1]
    [int]$left = $matches[2]
    [int]$top = $matches[3]
    [int]$width = $matches[4]
    [int]$height = $matches[5]
    for ([int]$h = $left + 1; $h -le $left + $width; $h++) {
        for ([int]$v = $top + 1; $v -le $top + $height; $v++) {
            $coord = "$($h),$($v)"
            if (-not $pixels.Add($coord)) {
                if (-not $dupes.Add($coord)) { 
                    #ignore
                }
            }
        }
    }
}
$dupes.Count
# after doing all of this, one claim will not have anything in the $dupes list
for ($i = 0; $i -lt $a.Count; $i++) {
    $b = $a[$i] -match '#([\d]+) @ ([\d]+),([\d]+): ([\d]+)x([\d]+)'
    [int]$id = $matches[1]
    [int]$left = $matches[2]
    [int]$top = $matches[3]
    [int]$width = $matches[4]
    [int]$height = $matches[5]
    $nodupes = $true
    for ([int]$h = $left + 1; $h -le $left + $width; $h++) {
        for ([int]$v = $top + 1; $v -le $top + $height; $v++) {
            $coord = "$($h),$($v)"
            if (-not $dupes.Add($coord)) {
                $nodupes = $false
            }
        }
    }
    if ($nodupes) { $id; $timer.Stop(); Write-Host $timer.Elapsed; exit; }
}

I was quite pleased with using the boolean state of the hashset addition to identify whether an element was present already (in part 1). Part 2 is just quick and dirty.

Edit: I added a timer as well.