Day 10: Elves Look, Elves Say

Day 10.1


In [24]:
def transform(s):
    t = s + '\\'
    say_list = ''
    counter = 1
    for i in range(len(s)):
        if t[i] == t[i + 1]:
            counter += 1
        else:
            say_list += str(counter) + t[i]
            counter = 1
    return say_list

Test


In [25]:
def test():
    assert(transform('1') == '11')
    assert(transform('11') == '21')
    assert(transform('21') == '1211')
    assert(transform('1211') == '111221')
    assert(transform('111221') == '312211')
test()

Solution


In [29]:
def iterate_transform(s, n):
    for _ in range(n):
        s = transform(s)
    return len(s)

In [30]:
iterate_transform('3113322113', 40)


Out[30]:
329356

Day 10.2


In [31]:
%%time
iterate_transform('3113322113', 50)


CPU times: user 5.64 s, sys: 12 ms, total: 5.65 s
Wall time: 5.65 s
Out[31]:
4666278