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

Python3

# -*- coding: utf-8 -*-
import re
coveredID = set()
def interaction(rect1, rect2):
    coverList = set()
    (minXrect1,minYrect1,maxXrect1,maxYrect1) = [rect1[1],rect1[2],rect1[1] + rect1[3],rect1[2] + rect1[4]]
    (minXrect2,minYrect2,maxXrect2,maxYrect2) = [rect2[1],rect2[2],rect2[1] + rect2[3],rect2[2] + rect2[4]]
    if(minXrect1 > maxXrect2 or minXrect2 > maxXrect1 or minYrect1>maxYrect2 or minYrect2>maxYrect1):
        return coverList
    minXcover = max(min(minXrect1,maxXrect2),min(minXrect2,maxXrect1))
    maxXcover = min(max(minXrect1,maxXrect2),max(minXrect2,maxXrect1))
    minYcover = max(min(minYrect1,maxYrect2),min(minYrect2,maxYrect1))
    maxYcover = min(max(minYrect1,maxYrect2),max(minYrect2,maxYrect1))
    
    for i in range(minXcover,maxXcover):
        for j in range(minYcover,maxYcover):
            coverList.add((i,j))
    coveredID.add(rect1[0])
    coveredID.add(rect2[0])
    return coverList

file = open("day3.txt","r")
slics = list()
for line in file:
    (boxId,xPos,yPos,width,height) = [t(s) for t,s in zip((int,int,int,int,int),re.search('^#([\d.]+) @ ([\d.]+),([\d.]+): ([\d.]+)x([\d.]+)',str(line)).groups())]
    slics.append([boxId,xPos,yPos,width,height])
file.close()

# part 1
cover = set()
for i in range(len(slics)-1):
    for j in range(i+1,len(slics)):
        coverList = interaction(slics[i],slics[j])
        if(len(coverList) > 0):
            for pos in coverList:
                cover.add(pos)
print(len(cover))

# part 2
for slic in slics:
    if(slic[0] not in coveredID):
        print(slic[0])