Explorar el Código

2020/24

master
Roderic Day hace 4 años
padre
commit
d6ff015392
Se han modificado 1 ficheros con 28 adiciones y 0 borrados
  1. +28
    -0
      y2020/p24.py

+ 28
- 0
y2020/p24.py Ver fichero

@@ -0,0 +1,28 @@
import collections
import re
import sys


def calc(pos):
is_black = pos in active
count = sum(pos + step in active for step in maps.values())
if is_black and count not in {1, 2}:
return False
elif not is_black and count == 2:
return True
return is_black


text = sys.stdin.read()
maps = {'w': 2, 'e': -2, 'nw': 1+1j, 'ne': -1+1j, 'sw': 1-1j, 'se': -1-1j}
grid = collections.defaultdict(bool)
for line in text.splitlines():
end = sum(map(maps.get, re.findall(r'(se|sw|nw|ne|e|w)', line)))
grid[end] = not grid[end]
active = {pos for pos in grid if grid[pos]}
print(len(active))


for _ in range(100):
active = {p + s for p in active for s in maps.values() if calc(p + s)}
print(len(active))

Cargando…
Cancelar
Guardar