day7
This commit is contained in:
parent
63d503e636
commit
9219259cf8
74
day07.py
Normal file
74
day07.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
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)
|
||||||
2000
inputs/input7
Normal file
2000
inputs/input7
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user