advent/2016/07-2.py
2017-12-12 13:57:20 -08:00

36 lines
741 B
Python

# Python!
import re
def compare_nets(supernets, hypernets):
m = re.findall(r'(?=((\w)(?!\2)\w\2))', supernets)
abas = [''.join(a[0]) for a in m]
if not abas:
return False
babs = [s[1] + s[0] + s[1] for s in abas]
for aba, bab in zip(abas, babs):
if re.search(aba, supernets) and re.search(bab, hypernets):
return True
return False
with open('07.txt') as file:
rows = file.read().splitlines()
hynet_seq = re.compile('\[[^\]]*\]')
count = 0
for row in rows:
valid = False
supernets = hynet_seq.sub('|', row)
hypernets = ''.join([h.group() + '|' for h in hynet_seq.finditer(row)])
if compare_nets(supernets, hypernets): count += 1
print(count)
print('Supernets:', supernets)
print('Hypernets:', hypernets, '\n')