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

VIEW FULL DISCUSSION
 

Part 1

import numpy as np

with open("input.txt") as f:
    claims = []
    for line in f:
        _, _, coords, dim = line.split()
        x, y = coords.split(",")[0], coords.split(",")[1][:-1]
        a, b = dim.split("x")
        claims.append((int(x), int(y), int(a), int(b)))

max_x = max(t[0]+t[2]+1 for t in claims)
max_y = max(t[1]+t[3]+1 for t in claims)

fabric = np.zeros((max_x, max_y), dtype=int)
for x, y, a, b in claims:
    fabric[x:x+a, y:y+b] += 1

print(len(fabric[fabric > 1]))

Part 2

candidates = set(list(range(len(claims))))

fabric = np.zeros((max_x, max_y), dtype=int)
for i, (x, y, a, b) in enumerate(claims):
    f = fabric[x:x+a, y:y+b]
    uniques = np.unique(f)
    if list(uniques) != [0]:
        for u in uniques:
            candidates.discard(u)
        candidates.discard(i)
    fabric[x:x+a, y:y+b] = i

print(candidates.pop()+1)
code of conduct - report abuse