Last active
December 3, 2018 17:25
-
-
Save synio-wesley/e44da43bafdb3609c5b9e9eaab50f28e to your computer and use it in GitHub Desktop.
AOC 2018 - Day 3 - Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Input for both parts | |
lines = [line.strip() for line in open('day3.txt')] | |
data = [] | |
for line in lines: | |
parts = line.split(' ') | |
id = parts[0] | |
x, y = [int(x) for x in parts[2].rstrip(':').split(',')] | |
w, h = [int(x) for x in parts[3].split('x')] | |
data.append([id, x, y, w, h]) | |
# Part 1 | |
fabric = {} | |
for d in data: | |
_, x, y, w, h = [value for value in d] | |
for i in range(x, x + w): | |
for j in range(y, y + h): | |
fabric[(i, j)] = fabric[(i, j)] + 1 if (i, j) in fabric else 1 | |
print(sum([1 for numDupes in fabric.values() if numDupes > 1])) | |
# Part 2 (geometry based) | |
fabric = {} | |
i = 0 | |
skip = [] | |
while i < len(data): | |
aid, ax, ay, aw, ah = data[i] | |
j = -1 | |
separated = True | |
while separated and j + 1 < len(data): | |
j += 1 | |
if i == j: | |
j += 1 | |
continue | |
bid, bx, by, bw, bh = data[j] | |
separated = ax >= bx + bw or ax + aw <= bx or ay >= by + bh or ay + ah <= by | |
if separated: | |
print(aid) | |
break | |
i += 1 | |
# Part 2 - Alternative (check all coordinates, worse performance) | |
fabric = {} | |
for d in data: | |
id, x, y, w, h = [value for value in d] | |
for i in range(x, x + w): | |
for j in range(y, y + h): | |
if (i, j) not in fabric: | |
fabric[(i, j)] = [id] | |
else: | |
fabric[(i, j)].append(id) | |
dupes = 0 | |
possibleClaims = {} | |
for coord in fabric: | |
if len(fabric[coord]) == 1 and fabric[coord][0] not in possibleClaims: | |
possibleClaims[fabric[coord][0]] = True | |
elif len(fabric[coord]) > 1: | |
for id in fabric[coord]: | |
possibleClaims[id] = False | |
for id in possibleClaims: | |
if possibleClaims[id]: | |
print(id) | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment