Working out solutions for Advent of Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """ Advent of Code
  2. December 3, puzzle A
  3. """
  4. import re
  5. def main(lines):
  6. """ Find the number of overlapped cells in an NxN square.
  7. Given a list of claims in the format
  8. #123 @ 4,5: 6x7
  9. where the first number is the claim number, the second number
  10. group is the x, y coordinates of the upper left corner, and
  11. the third number group is the x, y size of the rectanglular claim,
  12. find the number of cells in the square that have more than one
  13. claim attached to them.
  14. The first task is to find out how big the total square is!
  15. This will also verify that the regular expression works.
  16. Result: This cloth is exactly 1000 inches to a side.
  17. So: Generate a 1000x1000 table, one value for each cell.
  18. For each claim:
  19. * If the cell is unoccupied, mark with "c"
  20. * If the cell is occupied, mark with "X"
  21. Then iterate through the table and count the number of Xes.
  22. """
  23. # grp 1 grp 2 grp 3 grp 4 grp 5
  24. pattern = re.compile("#([0-9]+) @ ([0-9]+),([0-9]+): ([0-9]+)x([0-9]+)")
  25. # maxsize, claim = 0, 0 # only need this for determining cloth size
  26. # Generate a 1000x1000 list of empty ("") cells using list comprehensions
  27. fabric = [["" for y in range(1000)] for x in range(1000)] # fabric[y][x] (it's backwards!)
  28. for line in lines:
  29. m = pattern.match(line)
  30. # because xleft and ytop are "distance from the edge", they're zero-indexed
  31. xleft, ytop, xwidth, yheight = int(m.group(2)), int(m.group(3)), int(m.group(4)), int(m.group(5))
  32. xright = xleft + xwidth
  33. ybottom = ytop + yheight
  34. for j in range(ytop, ybottom): # ranges include the first value but not the second
  35. for i in range(xleft, xright):
  36. if fabric[j][i] == "": # if the cell is unclaimed
  37. fabric[j][i] = "c" # claim it
  38. else: # otherwise
  39. fabric[j][i] = "X" # mark it X so we know it's contested
  40. # a helper function, isX, will return 1 if a cell is "X" and 0 if it's not.
  41. contested = sum([sum([isX(cell) for cell in line]) for line in fabric])
  42. print("There are {} contested cells.".format(contested))
  43. # Commenting out the "find the size of the cloth" code, but leaving it in for historical purposes
  44. # if xright > maxsize:
  45. # maxsize = xright
  46. # claim = int(m.group(1))
  47. # if ybottom > maxsize:
  48. # maxsize = ybottom
  49. # claim = int(m.group(1))
  50. # print("Cloth is {} inches square, thanks to claim {}".format(maxsize, claim))
  51. def isX(cell):
  52. """ Returns 1 if the cell passed in contains "X", 0 otherwise.
  53. """
  54. return 1 if cell == "X" else 0
  55. if __name__ == "__main__":
  56. lines = []
  57. with open("03in.txt","r") as f:
  58. for line in f:
  59. lines.append(line.strip())
  60. main(lines)