### re: AoC Day 3: No Matter How You Slice It VIEW POST

Solved last night, refactored this morning! Actually pretty proud of this. Python solution:

``````import re

with open('input.txt', 'r') as f:
data = []
for line in f:
nums = [int(n) for n in re.findall(r'\d+', line)]
data.append({'id': nums, 'coordinates': [nums, nums], 'dimensions': [nums, nums]})

def get_coordinates(coordinates, dimensions):
for x in range(dimensions):
for y in range(dimensions):
yield str(x + coordinates) + "," + str(y + coordinates)

def get_overlaps(data):
overlaps = set()
filled = set()
for line in data:
for coord in get_coordinates(line['coordinates'], line['dimensions']):
if coord in filled:
overlaps.add(coord)
else:
filled.add(coord)
return overlaps

def no_overlaps(coordinates, dimensions, overlaps):
for coord in get_coordinates(coordinates, dimensions):
if coord in overlaps:
return False
return True

def find_no_overlaps(data, overlaps):
for line in data:
if no_overlaps(line['coordinates'], line['dimensions'], overlaps):
return line['id']

overlaps = get_overlaps(data)
# Q1
print(len(overlaps))

# Q2
print(find_no_overlaps(data, overlaps))
``````

I love how clean this solution is!

Thank you so much -- that means a lot (I've been super sad this morning because somebody was mean about my solution on Twitter).

🙄People can be the worst sometimes. Sorry you had to deal with that.

code of conduct - report abuse  