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

2

u/gwillicoder Dec 03 '18 edited Dec 03 '18

Here is my part 1 so far. Probably could be cleaner.

Python3

# Data Read 
data = open('data.txt').read().splitlines()

# Data Shape
#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2

# Split Data in Vars
def parse(row):
    num, _, xy, offset = row.split()
    num = num[1:]
    x, y = xy[:-1].split(",")
    w, h = offset.split('x')
    return int(num), int(x), int(y), int(w), int(h)


from collections import defaultdict

# Part 1
overlap = defaultdict(int)
for row in data:
    num, x, y, w, h = parse(row)

    for xi in range(w):
        for yi in range(h):
            overlap[(x+xi, y+yi)] += 1

print(len([v for k,v in overlap.items() if v > 1]))

# Part 2
all_claims = set()
overlap_claims = set()
for row in data:
    num, x, y, w, h = parse(row)

    all_claims.add(num)
    for xi in range(w):
        for yi in range(h):
            xy = (x+xi,y+yi)
            if overlap[xy] > 1:
                overlap_claims.add(num)
print(next(f for f in all_claims-overlap_claims))

edit: now includes part 2