2016-12-07 - Internet Protocol Version 7
(original .ipynb)
puzzle_input_str = open("puzzle_input/day7.txt").read()
test_input_str = """abba[mnop]qrst
abcd[bddb]xyyx
aaaa[qwer]tyui
ioxxoj[asdfgh]zxcvbn"""
def is_abba(s):
a1, b1, b2, a2 = s
return a1 == a2 and b1 == b2 and a1 != b1
def has_abba(s):
for i in range(len(s)-3):
if is_abba(s[i:i+4]):
return True
return False
def parse_line(line):
in_hypernet = False
sequences = []
hypernets = []
current = []
for c in line:
if c == "[":
in_hypernet = True
sequences.append("".join(current))
current = []
elif c == "]":
in_hypernet = False
hypernets.append("".join(current))
current = []
else:
current.append(c)
if current:
sequences.append("".join(current))
return sequences, hypernets
def supports_tls(sequences, hypernets):
seq_abba = any(has_abba(seq) for seq in sequences)
hyp_abba = any(has_abba(seq) for seq in hypernets)
return seq_abba and not hyp_abba
def part_one(input_str):
count = 0
for line in input_str.split("\n"):
sequences, hypernets = parse_line(line)
if supports_tls(sequences, hypernets):
count += 1
return count
assert 2 == part_one(test_input_str)
print("part one:", part_one(puzzle_input_str))
part one: 110
def is_aba(s):
a1, b, a2 = s
return a1 == a2 and a1 != b
def get_abas(s):
for i in range(len(s)-2):
maybe_aba = s[i:i+3]
if is_aba(maybe_aba):
yield maybe_aba
def to_bab(s):
a1, b, a2 = s
return f"{b}{a1}{b}"
def supports_ssl(sequences, hypernets):
for seq in sequences:
for aba in get_abas(seq):
bab = to_bab(aba)
for hyp in hypernets:
if bab in hyp:
return True
return False
def part_two(input_str):
count = 0
for line in input_str.split("\n"):
sequences, hypernets = parse_line(line)
if supports_ssl(sequences, hypernets):
count += 1
return count
print("part two:", part_two(puzzle_input_str))
part two: 242