From fd943e0d4931001495b59e7ae51f0b083cece9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Doblas=20Jim=C3=A9nez?= Date: Fri, 11 Mar 2022 11:42:09 +0100 Subject: [PATCH] Solution to problem 7 part 1 in Python --- src/Year_2016/P7.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/Year_2016/P7.py diff --git a/src/Year_2016/P7.py b/src/Year_2016/P7.py new file mode 100644 index 0000000..b1a6728 --- /dev/null +++ b/src/Year_2016/P7.py @@ -0,0 +1,53 @@ +# --- Day 7: Internet Protocol Version 7 --- + +# While snooping around the local network of EBHQ, you compile a list of IP +# addresses (they're IPv7, of course; IPv6 is much too limited). You'd like to +# figure out which IPs support TLS (transport-layer snooping). + +# An IP supports TLS if it has an Autonomous Bridge Bypass Annotation, or ABBA. +# An ABBA is any four-character sequence which consists of a pair of two +# different characters followed by the reverse of that pair, such as xyyx or +# abba. However, the IP also must not have an ABBA within any hypernet +# sequences, which are contained by square brackets. + +# For example: + +# abba[mnop]qrst supports TLS (abba outside square brackets). +# abcd[bddb]xyyx does not support TLS (bddb is within square brackets, even +# though xyyx is outside square brackets). +# aaaa[qwer]tyui does not support TLS (aaaa is invalid; the interior +# characters must be different). +# ioxxoj[asdfgh]zxcvbn supports TLS (oxxo is outside square brackets, even +# though it's within a larger string). + +# How many IPs in your puzzle input support TLS? + +# from collections import deque +# from itertools import islice #tee + +import re + +with open("files/P7.txt") as f: + IPs = [ + re.split(r"\[([^\]]+)\]", line) for line in f.read().strip().split() + ] + + +def abba(s: str) -> bool: + for i0, i1, i2, i3 in zip(s, s[1:], s[2:], s[3:]): + if i0 == i3 and i1 == i2 and i0 != i1: + return True + return False + + +def part_1() -> None: + count = 0 + for ip in IPs: + sn, hn = (" ".join(ip[::2]), " ".join(ip[1::2])) + if abba(sn) and not (abba(hn)): + count += 1 + print(count) + + +if __name__ == "__main__": + part_1()