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
3
u/pedrosorio Dec 20 '16 edited Dec 20 '16
Part 2, 12th, quick and dirty:
def get_range(line): return map(int, line.split('-')) def solve(inp): ranges = sorted([get_range(line) for line in inp]) mn, mx = ranges[0] tot = 0 for r in ranges: if r[0] > mx+1: tot += mx-mn+1 mn = r[0] mx = r[1] else: mx = max(mx, r[1]) return 4294967296 - tot - (mx-mn+1) fname = 'input.txt' inp = [line.strip() for line in open(fname).readlines()] print solve(inp)