Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

64 lines
1.6KB

  1. import collections
  2. import os
  3. import re
  4. import sys
  5. from pathlib import Path
  6. import requests
  7. def render(grid, brush=None):
  8. if brush is None:
  9. brush = {v: v for v in grid.values()}
  10. if isinstance(brush, str):
  11. brush = {i: c for i, c in enumerate(brush)}
  12. xmin, *_, xmax = sorted(int(p.real) for p in grid)
  13. ymin, *_, ymax = sorted(int(p.imag) for p in grid)
  14. brush[None] = ' '
  15. rendered = ''
  16. for y in range(ymin, ymax + 1):
  17. for x in range(xmin, xmax + 1):
  18. rendered += brush[grid.get(complex(x, y))]
  19. rendered += '\n'
  20. return rendered
  21. def read_image(text):
  22. grid = collections.defaultdict(str)
  23. for y, line in enumerate(text.splitlines()):
  24. for x, cell in enumerate(line):
  25. grid[complex(x, y)] = cell
  26. return grid
  27. def shortest_path(start, end, move):
  28. seen = {}
  29. edge = {start: None}
  30. while edge:
  31. seen.update(edge)
  32. edge = {
  33. adj: pos
  34. for pos in edge
  35. for adj in move(pos)
  36. if adj not in seen
  37. }
  38. if end in seen:
  39. break
  40. else:
  41. raise RuntimeError('Path not found', seen)
  42. path = []
  43. while end:
  44. path.append(end)
  45. end = seen[end]
  46. return path[::-1]
  47. def get_dat():
  48. path = sys.argv[1]
  49. year, qn = map(int, re.findall(r'\d+', sys.argv[1]))
  50. url = f'https://adventofcode.com/{year}/day/{qn}/input'
  51. cookies = {'session': os.environ['SESSION']}
  52. response = requests.get(url, cookies=cookies)
  53. response.raise_for_status()
  54. Path(path).write_bytes(response.content)