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

FULL DISCUSSION
 

Took a somewhat alternative approach to my python solution. Dictionaries felt more approachable so that is what I went with.

with open('input.txt') as file:
    data = file.read()

fabric = {}
for claim in data.splitlines():
    claim_data = claim.split()
    _id = claim_data[0][1:]
    x_pos = int(claim_data[2].split(',')[0])
    y_pos = int(claim_data[2].split(',')[1][:-1])
    width = int(claim_data[3].split('x')[0])
    height = int(claim_data[3].split('x')[1])

    for x_inch in range(width):
        for y_inch in range(height):
            new_cords = (x_pos+x_inch, y_pos+y_inch)
            try:
                fabric[new_cords].append(_id)
            except KeyError:
                fabric[new_cords] = [_id]
count = 0
for key in fabric.keys():
    if len(fabric[key]) > 1:
        count += 1

print(count, 'inches of fabric are within two or more claims')

For part 2 I leveraged the same dictionary logic and the following. Hoping the early returns from check_claim keep the code performant.

def check_claim(claim_data):
    _id = claim_data[0][1:]
    x_pos = int(claim_data[2].split(',')[0])
    y_pos = int(claim_data[2].split(',')[1][:-1])
    width = int(claim_data[3].split('x')[0])
    height = int(claim_data[3].split('x')[1])

    for x_inch in range(width):
        for y_inch in range(height):
            new_cords = (x_pos+x_inch, y_pos+y_inch)
            if fabric[new_cords] != [_id]:
                return False
    return True

for claim in data.splitlines():
    claim_data = claim.split()
    if check_claim(claim_data):
        print('Claim', claim_data[0][1:], 'has no overlapping claims')
        break
code of conduct - report abuse