Day 20 - Advent of Code 2016

Working solutions for the day 20 puzzles.

Part One

""" day_20_01.py """

# usage: python3 day_20_01.py < input

rules = []

while True:
    try:
        condition = input()
    except EOFError:
        break
    begin, end = map(int, condition.split('-'))
    rules.append((begin, end))

limit = pow(2, 32)

ip = 0
while ip < limit:
    for begin, end in rules:
        if begin <= ip <= end:
            ip = end + 1
            break
    else:
        break

print(ip)

Part Two

""" day_20_02.py """

# usage: python3 day_20_02.py < input

rules = []

while True:
    try:
        condition = input()
    except EOFError:
        break
    begin, end = map(int, condition.split('-'))
    rules.append((begin, end))

limit = pow(2, 32)

answer = 0
ip = 0
while ip < limit:
    for begin, end in rules:
        if begin <= ip <= end:
            ip = end + 1
            break
    else:
        answer += 1
        ip += 1

print(answer)