This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_ipv7.py
65 lines (48 loc) · 1.63 KB
/
07_ipv7.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
##############################################
# --- Day 7: Internet Protocol Version 7 --- #
##############################################
import AOCUtils
def splitNets(ipv7):
supernets, hypernets = [], []
for s in ipv7.split("["):
if "]" in s:
a, b = s.split("]")
hypernets.append(a)
supernets.append(b)
else:
supernets.append(s)
return supernets, hypernets
def supportsTLS(ipv7):
def hasABBA(s):
for i in range(len(s)-3):
if s[i] != s[i+1] and s[i] == s[i+3] and s[i+1] == s[i+2]:
return True
return False
supernets, hypernets = splitNets(ipv7)
return any(hasABBA(s) for s in supernets) and not any(hasABBA(h) for h in hypernets)
def supportsSSL(ipv7):
def getABA(s):
abas = []
for i in range(len(s)-2):
if s[i] != s[i+1] and s[i] == s[i+2]:
abas.append(s[i:i+3])
return abas
supernets, hypernets = splitNets(ipv7)
subSupernets = []
for supernet in supernets:
subSupernets += getABA(supernet)
subHypernets = []
for hypernet in hypernets:
subHypernets += getABA(hypernet)
for s in subSupernets:
for h in subHypernets:
if s[0] == h[1] == s[2] and h[0] == s[1] == h[2]:
return True
return False
##############################################
ipv7s = AOCUtils.loadInput(7)
tlsCount = sum(supportsTLS(ipv7) for ipv7 in ipv7s)
print("Part 1: {}".format(tlsCount))
tlsCount = sum(supportsSSL(ipv7) for ipv7 in ipv7s)
print("Part 2: {}".format(tlsCount))
AOCUtils.printTimeTaken()