My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 51 lines 1.1 kB view raw
1import re 2import fileinput 3 4 5def is_abba(seq): 6 for i in range(len(seq) - 3): 7 a, b, c, d = seq[i:i+4] 8 if a == d and b == c and a != b: 9 return True 10 11 return False 12 13 14def find_abas(seq): 15 for i in range(len(seq) - 2): 16 a, b, c = seq[i:i+3] 17 if a == c and a != b: 18 yield a, b 19 20 21def parse_address(address): 22 runs = re.findall(r'(\w+)', address.strip()) 23 return runs[::2], runs[1::2] 24 25 26def supports_tls(address): 27 sequences, hypernets = parse_address(address) 28 29 if any(is_abba(s) for s in sequences): 30 if not any(is_abba(h) for h in hypernets): 31 return True 32 33 return False 34 35 36def supports_ssl(address): 37 sequences, hypernets = parse_address(address) 38 39 for seq in sequences: 40 for a, b in find_abas(seq): 41 bab = b + a + b 42 43 if any(bab in h for h in hypernets): 44 return True 45 46 return False 47 48 49ADDRESSES = [line.strip() for line in fileinput.input()] 50print "Number of TLS IPs: %i" % sum(supports_tls(a) for a in ADDRESSES) 51print "Number of SSL IPs: %i" % sum(supports_ssl(a) for a in ADDRESSES)