Chellenge 6

Challenge 6.1


In [1]:
myinput = '/home/fmuinos/projects/adventofcode/2016/ferran/inputs/input6.txt'

In [2]:
! wc -l inputs/input6.txt


598 inputs/input6.txt

In [44]:
from collections import defaultdict

def parse_freqs(path):
    freq = defaultdict(lambda: defaultdict(int))
    with open(path,'rt') as f:
        for line in f:
            message = line.rstrip()
            for i in range(len(message)):
                freq[i][message[i]] += 1
    return freq

def most_freq(freq_dict, most=True):
    message = []
    for ind in freq_dict:
        freq_sort = iter(sorted(freq_dict[ind].items(), key=lambda i: i[1], reverse=most))
        message.append(next(freq_sort)[0])
    return ''.join(message)

def unnoise(myinput, most=True):
    freq = parse_freqs(myinput)
    return most_freq(freq, most)

In [45]:
unnoise(myinput)


Out[45]:
'asvcbhvg'

Challenge 6.2


In [46]:
unnoise(myinput, most=False)


Out[46]:
'odqnikqv'