選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

71 行
1.8KB

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