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

1

u/EdeMeijer Dec 03 '18

PostgreSQL query (omitted actual puzzle input, few lines as an example). Just because why not.

WITH input (claim) AS (
    VALUES
        ('#1 @ 286,440: 19x24'),
        ('#2 @ 430,120: 20x14'),
        ('#3 @ 250,746: 20x17'),
        ('#4 @ 639,255: 28x28'),
        ('#5 @ 793,21: 23x10'),
        ('#6 @ 835,164: 25x11'),
        ....
),
input_parsed AS (
    SELECT regexp_matches(claim, '^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)$', 'g')::INTEGER[] AS p
    FROM input
),
claims AS (
    SELECT
        p[1] AS id,
        p[2] AS x,
        p[3] AS y,
        p[4] AS width,
        p[5] AS height
    FROM
        input_parsed
),
cell_claims AS (
    SELECT c.id, x.x, y.y
    FROM
        generate_series(0, 1000) x
        CROSS JOIN generate_series(0, 1000) y
        JOIN claims c
            ON c.x <= x.x
            AND c.x + c.width > x.x
            AND c.y <= y.y
            AND c.y + c.height > y.y
)
SELECT 
    (
        SELECT COUNT(*)
        FROM (
            SELECT 1
            FROM cell_claims
            GROUP BY x, y
            HAVING COUNT(*) > 1
        ) d
    ) AS part1,
    (
        SELECT id
        FROM claims
        
        EXCEPT
        
        SELECT DISTINCT a.id
        FROM
            cell_claims a
            JOIN cell_claims b
                ON b.x = a.x
                AND b.y = a.y
                AND b.id <> a.id
    ) AS part2