-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.py
65 lines (42 loc) · 1.56 KB
/
day07.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
def read_input(filepath):
with open(filepath) as f:
return [line.strip() for line in f]
def split_inside_outside(str, start_char, end_char):
outside = [] # list of substrings outside of the brackets
inside = [] # list of substrings inside the brackets
s = str
while len(s) > 0:
pre, _, s = s.partition(start_char)
outside.append(pre)
if len(s) > 0:
hypernet, _, s = s.partition(end_char)
inside.append(hypernet)
return inside, outside
def is_ABBA(str):
return str[0] == str[3] and str[1] == str[2] and str[0] != str[1]
def has_ABBA(str):
return any(is_ABBA(str[i:i+4]) for i in range(0, len(str) - 3))
def support_TLS(str):
inside, outside = split_inside_outside(str, '[', ']')
return any(has_ABBA(o) for o in outside) and not any(has_ABBA(i) for i in inside)
def test_ABA_BAB(str, list):
for i in range(0, len(str) - 2):
slice = str[i:i+3]
if slice[0] == slice[2]:
bab = slice[1] + slice[0] + slice[1]
if any(bab in s for s in list):
return True
return False
def support_SSL(str):
inside, outside = split_inside_outside(str, '[', ']')
return any(test_ABA_BAB(o, inside) for o in outside)
def count_TLS(ips):
return sum(support_TLS(ip) for ip in ips)
def count_SSL(ips):
return sum(support_SSL(ip) for ip in ips)
if __name__ == "__main__":
ips = read_input("day07.txt")
ans = count_TLS(ips)
print("Part1: {}".format(ans))
ans2 = count_SSL(ips)
print("Part2: {}".format(ans2))