My solutions to 2018's advent of code
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

123456789101112131415161718192021222324252627282930
  1. import re
  2. from collections import defaultdict
  3. with open('data/03.txt', 'r') as file:
  4. cells = defaultdict(lambda: 0)
  5. claims = map(lambda l: map(int, re.findall(r'\d+', l)), file.readlines())
  6. ids = set()
  7. overlaps = 0
  8. for (cid, x, y, width, height) in claims:
  9. collision = False
  10. for i in range(width):
  11. for j in range(height):
  12. previous = cells[(x + i, y + j)]
  13. if previous == 0:
  14. cells[(x + i, y + j)] = cid
  15. elif previous == -1:
  16. collision = True
  17. else:
  18. collision = True
  19. overlaps += 1
  20. if previous in ids:
  21. ids.remove(previous)
  22. cells[(x + i, y + j)] = -1
  23. if not collision:
  24. ids.add(cid)
  25. print(overlaps)
  26. print(list(ids)[0])