-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2016_07.py
84 lines (66 loc) 路 1.81 KB
/
AoC2016_07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#! /usr/bin/env python3
#
# Advent of Code 2015 Day 7
#
import re
import sys
from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
ABBA = re.compile(r"([a-z])(?!\1)([a-z])\2\1")
HYPERNET = re.compile(r"\[([a-z]*)\]")
ABA = re.compile(r"([a-z])(?!\1)[a-z]\1")
TEST1 = """\
abba[mnop]qrst
abcd[bddb]xyyx
aaaa[qwer]tyui
ioxxoj[asdfgh]zxcvbn
abcox[ooooo]xocba
"""
TEST2 = """\
aba[bab]xyz
xyx[xyx]xyx
aaa[kek]eke
zazbz[bzb]cdb
"""
Input = InputData
Output1 = int
Output2 = int
class Solution(SolutionBase[Input, Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
return input_data
def part_1(self, inputs: InputData) -> int:
def is_tls(ip: str) -> bool:
for b in HYPERNET.findall(ip):
if ABBA.search(b) is not None:
return False
ip = ip.replace("[" + b + "]", "/")
return ABBA.search(ip) is not None
return sum(is_tls(line) for line in inputs)
def part_2(self, inputs: InputData) -> int:
def is_sls(ip: str) -> bool:
bs = HYPERNET.findall(ip)
for b in bs:
ip = ip.replace("[" + b + "]", "/")
return any(
any(bab in b for b in bs)
for bab in (
m.group()[1] + m.group()[0] + m.group()[1]
for i in range(len(ip) - 3)
for m in ABA.finditer(ip[i:])
)
)
return sum(is_sls(line) for line in inputs)
@aoc_samples(
(
("part_1", TEST1, 2),
("part_2", TEST2, 3),
)
)
def samples(self) -> None:
pass
solution = Solution(2016, 7)
def main() -> None:
solution.run(sys.argv)
if __name__ == "__main__":
main()