Solution to problem 3 part 1 in Python

This commit is contained in:
David Doblas Jiménez 2022-02-16 18:17:54 +01:00
parent 4e82781d8c
commit dd68c1e224

87
src/Year_2018/P3.py Normal file
View File

@ -0,0 +1,87 @@
# --- Day 3: No Matter How You Slice It ---
# The Elves managed to locate the chimney-squeeze prototype fabric for Santa's
# suit (thanks to someone who helpfully wrote its box IDs on the wall of the
# warehouse in the middle of the night). Unfortunately, anomalies are still
# affecting them - nobody can even agree on how to cut the fabric.
# The whole piece of fabric they're working on is a very large square - at
# least 1000 inches on each side.
# Each Elf has made a claim about which area of fabric would be ideal for
# Santa's suit. All claims have an ID and consist of a single rectangle with
# edges parallel to the edges of the fabric. Each claim's rectangle is defined
# as follows:
# The number of inches between the left edge of the fabric and the left
# edge of the rectangle.
# The number of inches between the top edge of the fabric and the top edge
# of the rectangle.
# The width of the rectangle in inches.
# The height of the rectangle in inches.
# A claim like #123 @ 3,2: 5x4 means that claim ID 123 specifies a rectangle 3
# inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4
# inches tall. Visually, it claims the square inches of fabric represented by
# # (and ignores the square inches of fabric represented by .) in the diagram
# below:
# ...........
# ...........
# ...#####...
# ...#####...
# ...#####...
# ...#####...
# ...........
# ...........
# ...........
# The problem is that many of the claims overlap, causing two or more claims to
# cover part of the same areas. For example, consider the following claims:
# #1 @ 1,3: 4x4
# #2 @ 3,1: 4x4
# #3 @ 5,5: 2x2
# Visually, these claim the following areas:
# ........
# ...2222.
# ...2222.
# .11XX22.
# .11XX22.
# .111133.
# .111133.
# ........
# The four square inches marked with X are claimed by both 1 and 2. (Claim 3,
# while adjacent to the others, does not overlap either of them.)
# If the Elves all proceed with their own plans, none of them will have enough
# fabric. How many square inches of fabric are within two or more claims?
import numpy as np
with open("files/P3.txt") as f:
claims = [line for line in f.read().strip().split("\n")]
def part_1() -> None:
grid = np.zeros((1000, 1000), dtype=int)
for claim in claims:
claim_id, _, orig, size = claim.split()
left, top = orig[:-1].split(",")
width, height = size.split("x")
for row in range(int(top), int(top) + int(height)):
for col in range(int(left), int(left) + int(width)):
grid[row][col] += 1
print(
f"There are {np.count_nonzero(grid >= 2)} square inches within two "
f"or more claims"
)
if __name__ == "__main__":
part_1()