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

46 行
1.2KB

  1. import re
  2. import sys
  3. from intcode import compute, parse
  4. from toolkit import read_image
  5. # part1
  6. text = sys.stdin.read()
  7. snapshot = ''.join(chr(val) for val in compute(text, 0))
  8. grid = read_image(snapshot)
  9. alignment_parameters = [
  10. (pos.real, pos.imag)
  11. for pos, val in grid.items()
  12. if {grid.get(pos + d) for d in [0, 1, -1, 1j, -1j]} == {'#'}
  13. ]
  14. print(int(sum(a * b for a, b in alignment_parameters)))
  15. # find path
  16. pos = {v: k for k, v in grid.items()}['^']
  17. ori = -1j
  18. path = []
  19. while {grid[pos + ori * rot] for rot in [1, 1j, -1j]} != {'.'}:
  20. if grid[pos + ori] == '#':
  21. pos += ori
  22. path[-1] += 1
  23. else:
  24. for c, rot in [('R', 1j), ('L', -1j)]:
  25. if grid[pos + ori * rot] == '#':
  26. ori *= rot
  27. path += [c, 0]
  28. full_path = ','.join(str(c) for c in path) + ','
  29. # compress
  30. regex = r'^(.{2,21})(?:\1)*(.{2,21})(?:\1|\2)*(.{2,21})(?:\1|\2|\3)*$'
  31. A, B, C = [group.strip(',') for group in re.match(regex, full_path).groups()]
  32. MAIN = full_path.replace(A, 'A').replace(B, 'B').replace(C, 'C').strip(',')
  33. # part2
  34. mem = parse(text)
  35. mem[0] += 1
  36. code = ('\n'.join([MAIN, A, B, C, 'n']) + '\n').encode()
  37. output = ''.join(chr(val) for val in compute(mem, iter(code)))
  38. print(ord(output[-1]))