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/ka-splam Dec 03 '18

Seeing that, I went back and re-wrote it in terms of System.Drawing.Rectangle.IntersectsWith and dropped it down to ~2.8s runtime on mine:

$lines = [system.io.file]::ReadAllLines('d:\aoc\2018\3\data.txt')
Add-Type -AssemblyName system.drawing

$claims = @{}
$clash = @{}

foreach($line in $lines)
{
    $id, $_, [int]$x, [int]$y, $_, [int]$w, [int]$h, $_ = $line.split(' :x,')

    $claims[$id] = [System.Drawing.Rectangle]::new($x, $y, $w, $h)
    $clash[$id] = 1
}
while ($clash.Count -gt 1)
{
    $claims.Keys.Where{$clash[$_]}.ForEach{
        $overlaps = foreach ($k in $clash.Keys) {
            if ($_ -ne $k -and $claims[$_].intersectsWith($claims[$k]))
            {
                $k
            }
        }
        foreach ($o in $overlaps) { $clash.Remove($o) }
    }
}
$clash.Keys

Instead of ~2M comparisons it now "only" does ~500k.