Working out solutions for Advent of Code
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """ Advent of Code
  2. December 3, puzzle B
  3. """
  4. import re
  5. def main(lines):
  6. """ Find the one claim that DOESN'T overlap 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. So: Generate a 1000x1000 table, one value for each cell.
  15. For each claim:
  16. * If the cell is unoccupied, mark with "c"
  17. * If the cell is occupied, mark with "X"
  18. Then cycle through the claims AGAIN. For each claim:
  19. * If any of its cells are marked "X", continue.
  20. * If ALL its cells are marked "c", that's my claim; report it!
  21. """
  22. # grp 1 grp 2 grp 3 grp 4 grp 5
  23. pattern = re.compile("#([0-9]+) @ ([0-9]+),([0-9]+): ([0-9]+)x([0-9]+)")
  24. # Generate a 1000x1000 list of empty ("") cells using list comprehensions
  25. fabric = [["" for y in range(1000)] for x in range(1000)] # fabric[y][x] (it's backwards!)
  26. # first cycle, to mark claims
  27. for line in lines:
  28. m = pattern.match(line)
  29. claim = int(m.group(1))
  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. # second cycle, to check claims
  41. for line in lines:
  42. m = pattern.match(line)
  43. claim = int(m.group(1))
  44. xleft, ytop, xwidth, yheight = int(m.group(2)), int(m.group(3)), int(m.group(4)), int(m.group(5))
  45. xright = xleft + xwidth
  46. ybottom = ytop + yheight
  47. contested = False
  48. for j in range(ytop, ybottom): # ranges include the first value but not the second
  49. for i in range(xleft, xright):
  50. if fabric[j][i] == "X": # if the cell is contested
  51. contested = True # oh well
  52. break # break out
  53. # otherwise continue
  54. if contested == False:
  55. print("Claim {} does not overlap!".format(claim))
  56. return
  57. # (I don't need isX anymore.)
  58. if __name__ == "__main__":
  59. lines = []
  60. with open("03in.txt","r") as f:
  61. for line in f:
  62. lines.append(line.strip())
  63. main(lines)