Solution to problem 3 in Python

This commit is contained in:
David Doblas Jiménez 2022-02-17 09:22:06 +01:00
parent dd68c1e224
commit b3c70d7c3d

View File

@ -66,7 +66,7 @@ with open("files/P3.txt") as f:
claims = [line for line in f.read().strip().split("\n")]
def part_1() -> None:
def part_1() -> np.array:
grid = np.zeros((1000, 1000), dtype=int)
for claim in claims:
@ -82,6 +82,37 @@ def part_1() -> None:
f"or more claims"
)
return grid
# --- Part Two ---
# Amidst the chaos, you notice that exactly one claim doesn't overlap by even a
# single square inch of fabric with any other claim. If you can somehow draw
# attention to it, maybe the Elves will be able to make Santa's suit after all!
# For example, in the claims above, only claim 3 is intact after all claims are
# made.
# What is the ID of the only claim that doesn't overlap?
def part_2() -> None:
filled_grid = part_1()
for claim in claims:
claim_id, _, orig, size = claim.split()
left, top = orig[:-1].split(",")
width, height = size.split("x")
counts = 0
for row in range(int(top), int(top) + int(height)):
for col in range(int(left), int(left) + int(width)):
if filled_grid[row][col] == 1:
counts += 1
if counts == (int(width) * int(height)):
print(f"The ID that does not overlap is {claim_id}")
if __name__ == "__main__":
part_1()
# part_1()
part_2()