Day 7 - Advent of Code 2016
Working solutions for the day 7 puzzles.
Part One
""" day_07_01.py """
# usage: python3 day_07_01.py < input
def abba(text):
""" check for abba pattern """
return text[0] == text[3] and text[1] == text[2] and text[0] != text[1]
def tls(text):
""" does text support tls per puzzle description """
legit = False
hypernet = False
for i, char in enumerate(text):
if char == '[':
hypernet = True
continue
if char == ']':
hypernet = False
continue
chunk = text[i:i + 4]
if '[' in chunk:
continue
if ']' in chunk:
continue
if len(chunk) < 4:
break
if abba(chunk):
if hypernet:
return False
legit = True
return legit
count = 0
while True:
try:
ip = input()
except EOFError:
break
if tls(ip):
count += 1
print(count)
Part Two
""" day_07_02.py """
# usage: python3 day_07_02.py < input
def aba(text):
""" check for aba pattern """
return text[0] == text[2] and text[0] != text[1]
def bab(text):
""" calculate bab from aba """
return text[1] + text[0] + text[1]
def ssl(text):
""" does text support ssl per puzzle description """
inner = set()
outer = set()
hypernet = False
for i, char in enumerate(text):
if char == '[':
hypernet = True
continue
if char == ']':
hypernet = False
continue
chunk = text[i:i + 3]
if '[' in chunk:
continue
if ']' in chunk:
continue
if len(chunk) < 3:
break
if aba(chunk):
if hypernet:
inner.add(chunk)
else:
outer.add(chunk)
for chunk in inner:
if bab(chunk) in outer:
return True
return False
count = 0
while True:
try:
ip = input()
except EOFError:
break
if ssl(ip):
count += 1
print(count)