Solutions to Advent of Code 2017 https://adventofcode.com/2017/
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.

12.py 899B

12345678910111213141516171819202122
  1. with open('data/12.txt', 'r') as file:
  2. pipes = list(map(lambda l: l.strip().replace(' <-> ', ', ').split(', '), file.readlines()))
  3. groups = [set('0')]
  4. while len(pipes):
  5. for gi, group in enumerate(groups):
  6. last_size = 0
  7. while len(group) != last_size:
  8. last_size = len(group)
  9. remove = []
  10. for pipeset in pipes:
  11. if any(program in group for program in pipeset):
  12. group = group.union(pipeset)
  13. groups[gi] = group
  14. remove.append(pipeset)
  15. for pipeset in remove:
  16. pipes.remove(pipeset)
  17. if len(pipes):
  18. pipeset = pipes[0]
  19. groups.append(set(pipeset))
  20. pipes.remove(pipeset)
  21. print(f'Part one: {len(groups[0])}')
  22. print(f'Part two: {len(groups)}')