Challenge 16


In [42]:
initial_state = '01111010110010011'
disk_length = 272

Solution 16.1


In [40]:
def dragon_iterator(initial_state):
    a = [int(i) for i in list(initial_state)]
    while len(a) > 0:
        b = [1 - int(i) for i in reversed(a)]
        a = a + [0] + b
        yield(''.join([str(i) for i in a]))        

def dragon_checksum(intial_state, disk_length):
    data = initial_state
    gen = dragon_iterator(initial_state)
    while len(data) < disk_length:
        data = next(gen)
    data = data[:disk_length]
    while len(data) % 2 == 0:
        pair_list = [data[2*i:2*(i+1)] for i in range(len(data)//2)]
        data = ''.join([str(2-len(set(item))) for item in pair_list])
    return data

Result


In [43]:
%%time
s = dragon_checksum(initial_state, disk_length)
print(s)
print(len(s))


00100111000101111
17
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 1.02 ms

Solution 16.2


In [44]:
%%time
new_disk_length = 35651584
t = dragon_checksum(initial_state, new_disk_length)
print(t)
print(len(t))


11101110011100110
17
CPU times: user 47.3 s, sys: 2.15 s, total: 49.5 s
Wall time: 49.5 s