My solutions to 2018's 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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

03.py 899B

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])