from tools.aoc import AOCDay from typing import Any class Day(AOCDay): inputs = [ [ (105, "input7"), ], [ (258, "input7"), ], ] def parse_input(self) -> list[tuple[list[str], list[str]]]: ips = [] for line in self.getInput(): ip = [] hypernets = [] for sp in line.split("["): if "]" not in sp: ip.append(sp) else: hypernet, sp_ip = sp.split("]") hypernets.append(hypernet) ip.append(sp_ip) ips.append((ip, hypernets)) return ips def part1(self) -> Any: def has_abba(sequence: str) -> bool: abba_found = False for x in range(len(sequence) - 3): if ( sequence[x] == sequence[x + 3] and sequence[x + 1] == sequence[x + 2] and sequence[x] != sequence[x + 1] ): abba_found = True return abba_found ans = 0 for ips, hypernets in self.parse_input(): if any(has_abba(i) for i in ips) and not any(has_abba(h) for h in hypernets): ans += 1 return ans def part2(self) -> Any: ans = 0 for ips, hypernets in self.parse_input(): found_aba_bab = False for ip in ips: for i in range(len(ip) - 2): if ( ip[i] == ip[i + 2] and ip[i] != ip[i + 1] and any(ip[i + 1] + ip[i] + ip[i + 1] in h for h in hypernets) ): found_aba_bab = True break if found_aba_bab: break if found_aba_bab: ans += 1 return ans if __name__ == "__main__": day = Day(2016, 7) day.run(verbose=True)