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.

91 line
2.2KB

  1. import collections
  2. import hashlib
  3. import itertools
  4. import os
  5. import re
  6. import sys
  7. from pathlib import Path
  8. import requests
  9. def render(grid, brush=None):
  10. if brush is None:
  11. brush = {v: v for v in grid.values()}
  12. if isinstance(brush, str):
  13. brush = {i: c for i, c in enumerate(brush)}
  14. xmin, *_, xmax = sorted(int(p.real) for p in grid)
  15. ymin, *_, ymax = sorted(int(p.imag) for p in grid)
  16. brush[None] = ' '
  17. rendered = ''
  18. for y in range(ymin, ymax + 1):
  19. for x in range(xmin, xmax + 1):
  20. rendered += brush[grid.get(complex(x, y))]
  21. rendered += '\n'
  22. return rendered
  23. def read_image(text):
  24. grid = collections.defaultdict(str)
  25. for y, line in enumerate(text.splitlines()):
  26. for x, cell in enumerate(line):
  27. grid[complex(x, y)] = cell
  28. return grid, x + 1, y + 1
  29. def shortest_path(start, end, move):
  30. seen = {}
  31. edge = {start: None}
  32. while edge:
  33. seen.update(edge)
  34. edge = {
  35. adj: pos
  36. for pos in edge
  37. for adj in move(pos)
  38. if adj not in seen
  39. }
  40. if end in seen:
  41. break
  42. else:
  43. raise RuntimeError('Path not found', seen)
  44. path = []
  45. while end:
  46. path.append(end)
  47. end = seen[end]
  48. return path[::-1]
  49. def get_dat():
  50. path = sys.argv[1]
  51. year, qn = map(int, re.findall(r'\d+', sys.argv[1]))
  52. url = f'https://adventofcode.com/{year}/day/{qn}/input'
  53. cookies = {'session': os.environ['SESSION']}
  54. response = requests.get(url, cookies=cookies)
  55. response.raise_for_status()
  56. Path(path).write_bytes(response.content)
  57. def batch(iterable, size):
  58. count = itertools.count()
  59. for _, sub in itertools.groupby(iterable, lambda _: next(count) // size):
  60. yield sub
  61. def md5(string):
  62. return hashlib.md5(string.encode()).hexdigest()
  63. def loop_consume(lines, handler):
  64. instructions = collections.deque(lines)
  65. count = 0
  66. while instructions:
  67. ok = handler(instructions[0])
  68. if ok:
  69. instructions.popleft()
  70. count = 0
  71. elif count < len(instructions):
  72. instructions.rotate(1)
  73. count += 1
  74. else:
  75. raise RuntimeError('Reached steady state')