In an effort to stretch my programming muscles, I'm doing the Advent of Code 2018 in a language new to me: Python. Coming from a C# background, I'm probably making all kinds of mistakes or are unaware of some usefull things in Python. This is my solution to day 3, and I would like to get a review on my code. I'm interested in both better ways to solve the problem using Python's tools, but also anything that has to do with style (I tried applying PEP8 rules), readability, etc. Thanks!
The full challenge is here and a bit involved, I'll try to keep it a bit shorter here.
The Elves managed to locate the chimney-squeeze prototype fabric for Santa's suit. Unfortunately, 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: 5x4means 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. 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?
This puzzle also comes with custom puzzle input. I can provide mine, but it's 1350 lines long. The example above provides the way the input is formatted.
class Claim(object): id = None x = None y = None width = None height = None def __init__(self, claim_id, x, y, width, height): self.id = claim_id self.x = x self.y = y self.width = width self.height = height def __repr__(self): return "<Claim #%s - %s, %s - %sx%s>" % (self.id, self.x, self.y, self.width, self.height) def read_file_lines(file_path, strip_lines=True): """ Reads the specified file and returns it's lines an array file_path: the path to the file strip_lines (default: true): boolean to indicate whether or not to strip leading and trailing whitespace from each line Returns: An array of the lines in the file as string """ with open(file_path, "r") as f: if strip_lines: return [l.strip() for l in f.readlines()] return [l for l in f.readlines()] def parse_input(lines): claims =  for line in lines: parts = line.split(" ") id = int(parts[1:]) x = int(parts.split(",")) y = int(parts.split(",")[:-1]) width = int(parts.split("x")) height = int(parts.split("x")) claims.append(Claim(id, x, y, width, height)) return claims def generate_matrix(size): return [*size for _ in range(size)] def print_matrix(matrix): line = "" for y in range(0, len(matrix)): line = line + str(y) + ": " for x in range(0, len(matrix)): line = line + str(matrix[x][y]) print(line) line = "" if __name__ == '__main__': content = read_file_lines("input.txt") claims = parse_input(content) matrix = generate_matrix(1000) print_matrix(matrix) for claim in claims: x_indexes = range(claim.x, claim.x + claim.width) y_indexes = range(claim.y, claim.y + claim.height) for x in x_indexes: for y in y_indexes: matrix[x][y] = matrix[x][y] + 1 print_matrix(matrix) inches_double_claimed = 0 for x in range(0, len(matrix)): for y in range(0, len(matrix)): if matrix[x][y] >= 2: inches_double_claimed += 1 print("Inches claimed by two or more claims:", inches_double_claimed)