-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent2016_7.py
86 lines (70 loc) · 2.14 KB
/
advent2016_7.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
85
86
#!/usr/bin/env python
# http://adventofcode.com/2016
import re
import logging as log
log.basicConfig(level=log.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
def supports_tls(s):
if has_abba(s)[0]:
if not abba_inside_square_brackets(s, has_abba(s)[1]):
return True
return False
def supports_ssl(s):
has, bab = has_bab(s)
# antibab = bab[1] + bab[0] + bab[1]
if has:
for b in bab:
if bab_inside_square_brackets(s, b):
return True
return False
def has_bab(s, pattern=None):
if pattern:
if pattern in s:
return True, pattern
else:
return False, None
results = []
outside_square = re.findall(r"(.*?)(?:\[.*?\]|$)", s)
for sampleout in outside_square:
index = 0
while index + 2 < len(sampleout):
if (
sampleout[index] == sampleout[index + 2]
and sampleout[index] != sampleout[index + 1]
):
results.append(sampleout[index : index + 3])
index += 1
if results:
return True, results
else:
return False, None
def has_abba(s):
index = 0
while index + 3 < len(s):
if s[index] == s[index + 1]:
index += 1
continue
if s[index] == s[index + 3] and s[index + 1] == s[index + 2]:
return True, s[index : index + 4]
index += 1
return False, None
def bab_inside_square_brackets(s, bab):
antipattern = bab[1] + bab[0] + bab[1]
in_square = re.findall(r"\[(.*?)\]", s)
for sample in in_square:
if has_bab(sample, pattern=antipattern)[0]:
return True
return False
def abba_inside_square_brackets(s, abba):
in_square = re.findall(r"\[(.*?)\]", s)
for sample in in_square:
if has_abba(sample)[0]:
return True
return False
if __name__ == "__main__":
results = []
# print(has_bab('aba'))
with open("advent2016-7_input.txt") as file:
for line in file:
line = line.rstrip("\r\n")
results.append(supports_ssl(line))
print(results.count(True))