Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import functools
  2. import itertools
  3. import math
  4. import re
  5. import sys
  6. def render(grid):
  7. lines = []
  8. for row in grid:
  9. for lns in zip(*[cell.splitlines()[1:-1] for cell in row]):
  10. lines.append(''.join(ln[1:-1] for ln in lns))
  11. return '\n'.join(lines)
  12. class Ops:
  13. noop = lambda txt: txt # noqa
  14. flipV = lambda txt: '\n'.join(ln for ln in txt.splitlines()[::-1]) # noqa
  15. flipH = lambda txt: '\n'.join(ln[::-1] for ln in txt.splitlines()) # noqa
  16. transpose = lambda txt: '\n'.join(map(''.join, zip(*txt.splitlines()))) # noqa
  17. def variants(string):
  18. alts = [Ops.flipH, Ops.flipV, Ops.transpose]
  19. for ops in itertools.product(*[[Ops.noop, fn] for fn in alts]):
  20. yield functools.reduce(lambda s, f: f(s), ops, string)
  21. def search_in(source, target):
  22. source_lns = source.splitlines()
  23. target_lns = target.splitlines()
  24. h, w = len(target_lns), len(target_lns[0])
  25. for y in range(len(source_lns)):
  26. for x in range(len(source_lns[0])):
  27. window = '\n'.join(ln[x:x + w] for ln in source_lns[y:y + h])
  28. if re.fullmatch(target, window):
  29. yield (x, y)
  30. def find(old, new):
  31. delta = new - old
  32. base = fixed[old]
  33. if delta == -1: goal = [ln[0] for ln in base.splitlines()] # noqa
  34. elif delta == 1: goal = [ln[-1] for ln in base.splitlines()] # noqa
  35. elif delta == 1j: goal = base.splitlines()[-1] # noqa
  36. elif delta == -1j: goal = base.splitlines()[0] # noqa
  37. for tid, tile in tiles.items():
  38. for var in variants(tile):
  39. if delta == -1: found = [ln[-1] for ln in var.splitlines()] # noqa
  40. elif delta == 1: found = [ln[0] for ln in var.splitlines()] # noqa
  41. elif delta == 1j: found = var.splitlines()[0] # noqa
  42. elif delta == -1j: found = var.splitlines()[-1] # noqa
  43. if goal == found:
  44. tiles.pop(tid)
  45. grid[new] = tid
  46. fixed[new] = var
  47. yield tid
  48. return
  49. text = sys.stdin.read().strip()
  50. tiles = {}
  51. for line in text.split('\n\n'):
  52. title, content = line.split(':\n')
  53. tid = int(title.split()[1])
  54. tiles[tid] = content
  55. size = int(len(tiles) ** 0.5)
  56. start = min(tiles, key=tiles.get)
  57. fixed = {0: tiles.pop(start)}
  58. grid = {0: start}
  59. while tiles:
  60. grid.update({
  61. new: good
  62. for old in list(grid)
  63. for new in {old - 1, old + 1, old + 1j, old - 1j} - set(fixed)
  64. for good in find(old, new)
  65. })
  66. xs = xmin, *_, xmax = sorted({int(p.real) for p in fixed})
  67. ys = ymin, *_, ymax = sorted({int(p.imag) for p in fixed})
  68. print(math.prod(grid[x + 1j * y] for x in [xmin, xmax] for y in [ymin, ymax]))
  69. monster = '''
  70. ..................#.
  71. #....##....##....###
  72. .#..#..#..#..#..#...
  73. '''[1:-1]
  74. src = render([[fixed[complex(x, y)] for x in xs] for y in ys])
  75. found = max([list(search_in(pic, monster)) for pic in variants(src)], key=len)
  76. print(src.count('#') - len(found) * monster.count('#'))