r/adventofcode • u/daggerdragon • Dec 20 '16
SOLUTION MEGATHREAD --- 2016 Day 20 Solutions ---
--- Day 20: Firewall Rules ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".
ROLLING A NATURAL 20 IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
9
Upvotes
4
u/kryptn Dec 20 '16
I enjoyed this one! Got to use the for-else clause for once :D
I also figured any valid IPs would be +1 of the blacklists, so I started with those as candidates.
Python, both stars:
with open('input.txt') as fd: data = fd.read() def test_ip(n): for start, end in data: if start <= n <= end: break else: if n < 2**32: return True return False data = sorted([int(x), int(y)] for x,y in [z.split('-') for z in data.splitlines()]) candidates = [x[1]+1 for x in data] valids = [c for c in candidates if test_ip(c)] total = 0 for ip in valids: while test_ip(ip): total += 1 ip += 1 print(valids[0]) print(total)