73 lines
1.6 KiB
Python
73 lines
1.6 KiB
Python
from lib import *
|
|
|
|
data = open(0).read().strip()
|
|
first = False
|
|
|
|
def has_abba(xs):
|
|
for i in range(len(xs) - 3):
|
|
if xs[i] == xs[i + 3] and xs[i + 1] == xs[i + 2] and xs[i] != xs[i + 1]:
|
|
return True
|
|
return False
|
|
|
|
if first:
|
|
r = 0
|
|
for line in data.splitlines():
|
|
is_abba = False
|
|
|
|
xs = ""
|
|
for c in line:
|
|
if c == "[":
|
|
if has_abba(xs):
|
|
is_abba = True
|
|
xs = ""
|
|
elif c == "]":
|
|
if has_abba(xs):
|
|
is_abba = False
|
|
xs = ""
|
|
break
|
|
xs = ""
|
|
xs += c
|
|
|
|
if xs != "" and has_abba(xs):
|
|
is_abba = True
|
|
|
|
if is_abba:
|
|
r += 1
|
|
print(r)
|
|
else:
|
|
def abas(xs) -> set:
|
|
r = set()
|
|
for i in range(len(xs) - 2):
|
|
if not in_square and xs[i] == xs[i + 2] and xs[i] != xs[i + 1]:
|
|
r.add(xs[i:i+3])
|
|
return r
|
|
|
|
def babs(xs) -> set:
|
|
r = set()
|
|
for i in range(len(xs) - 2):
|
|
if not in_square and xs[i] == xs[i + 2] and xs[i] != xs[i + 1]:
|
|
r.add(xs[i + 1] + xs[i] + xs[i + 1])
|
|
return r
|
|
|
|
r = 0
|
|
for line in data.splitlines():
|
|
aba, bab = set(), set()
|
|
in_square = False
|
|
xs = ""
|
|
for c in line:
|
|
if c == "[":
|
|
aba |= abas(xs)
|
|
xs = ""
|
|
elif c == "]":
|
|
bab |= babs(xs)
|
|
xs = ""
|
|
else:
|
|
xs += c
|
|
if xs != "":
|
|
aba |= abas(xs)
|
|
|
|
if aba & bab:
|
|
r += 1
|
|
|
|
print(r)
|