Day 10 - Advent of Code 2015

Working solutions for the day 10 puzzles.

Part One

""" day_10_01.py """

# usage: python3 day_10_01.py < input


def rle(text):
    """ encode using run length model """
    output = []
    current = None
    count = 0
    for term in text:
        if term == current:
            count += 1
        else:
            if count > 0:
                output.append((current, count))
            current = term
            count = 1
    if count > 0:
        output.append((current, count))
    return output


def rle_to_text(rle_obj):
    """ generate text from run length encoding """
    return ''.join([str(frequency) + symbol for symbol, frequency in rle_obj])


sequence = input()
for i in range(40):
    encoding = rle(sequence)
    sequence = rle_to_text(encoding)
print(len(sequence))

Part Two

""" day_10_02.py """

# usage: python3 day_10_02.py < input


def rle(text):
    """ encode using run length model """
    output = []
    current = None
    count = 0
    for term in text:
        if term == current:
            count += 1
        else:
            if count > 0:
                output.append((current, count))
            current = term
            count = 1
    if count > 0:
        output.append((current, count))
    return output


def rle_to_text(rle_obj):
    """ generate text from run length encoding """
    return ''.join([str(frequency) + symbol for symbol, frequency in rle_obj])


sequence = input()
for i in range(50):
    encoding = rle(sequence)
    sequence = rle_to_text(encoding)
print(len(sequence))