Advent of Code 2016 solutions https://adventofcode.com/2016/
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.

22.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/python3
  2. """Solution for day 22 of Advent of Code 2016.
  3. Part 1 is straight-forward reading of the data, generating pairs using itertools.permutations, and counting how
  4. many match the criteria.
  5. As is traditional at about this point in Advent of Code, part 2 requires you to make annoying assumptions based on
  6. the vague example in the question and some properties of your input that you have no guarantee are universal.
  7. In this case, the input needs to be partitioned in to three sets: empty nodes (of which there is just one), giant
  8. nodes (whose data can't possibly be placed anywhere else) and normal nodes (who can shift their data to the empty
  9. node, but not to any other normal node).
  10. When the nodes are printed out according to their type (as in the example), it becomes obvious that there's a "wall"
  11. of giant nodes in the way. As the only valid moves involve the empty node, you basically have to move that around and
  12. use it to move the data along the top row.
  13. For my input, it takes 34 moves to rearrange things and then move the target data left by one:
  14. ..............23456789012345678901234
  15. ..............1......................
  16. ..............0######################
  17. ..............9876543................
  18. ....................2................
  19. ....................1................
  20. ...................._................
  21. .....................................
  22. .....................................
  23. Then for each node to the left it takes an additional five moves, as the empty space loops around our target:
  24. ....45_..
  25. ....321..
  26. .........
  27. It needs to move a total of 35 nodes left, so 34+35*5 total moves = 209.
  28. """
  29. import re
  30. import itertools
  31. matcher = re.compile(r'^/dev/grid/node-x([0-9]+)-y([0-9]+)\s+([0-9]+)T\s+([0-9]+)T\s+([0-9]+)T\s+.*$')
  32. # Constants used for convenient access the groups in the matcher above
  33. locx = 0
  34. locy = 1
  35. size = 2
  36. used = 3
  37. free = 4
  38. with open('data/22.txt', 'r') as data:
  39. discs = [tuple(map(int, matcher.match(l).groups())) for l in data.readlines() if matcher.match(l)]
  40. pairs = sum(1 for p in itertools.permutations(discs, 2) if 0 < p[0][used] <= p[1][free])
  41. print('Part one: %s' % pairs)
  42. print('Part two: ...')
  43. grid = dict((disc[:locy+1], disc) for disc in discs)
  44. for y in range(27):
  45. print(''.join(['X' if (x,y) not in grid
  46. else '_' if grid[(x, y)][used] == 0
  47. else '#' if grid[(x, y)][used] > 150 # Arbitrary cut-off for "normal" vs "giant" nodes
  48. else '.' for x in range(37)]))