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 34 lines 770 B view raw
1import fileinput 2from utils import parse_line 3 4MAX_IP = 2**32 - 1 5 6 7def allowed_ips(blacklist, max_ip=MAX_IP): 8 """Generator over all allowed IPs given a blacklist.""" 9 i = 0 10 11 for start, end in sorted(blacklist): 12 if i < start: 13 for n in range(i, start): 14 yield n 15 16 if i <= end: 17 i = end + 1 18 19 if i <= max_ip: 20 for n in range(i, max_ip + 1): 21 yield n 22 23 24if __name__ == '__main__': 25 blacklist = [] 26 27 for line in fileinput.input(): 28 start, end = parse_line(r'(\d+)-(\d+)', line) 29 blacklist.append((start, end)) 30 31 whitelist = list(allowed_ips(blacklist)) 32 33 print "Lowest-valued whitelisted IP:", whitelist[0] 34 print "Number of allowed addresses:", len(whitelist)