<jupyter_start><jupyter_text>Chellenge 4 Challenge 4.1<jupyter_code>myinput = '/home/fmuinos/projects/adventofcode/2016/ferran/inputs/input4.txt'
! wc -l inputs/input4.txt
def name_id_check(room):
    name = '-'.join(room.split('-')[:-1])
    tail = room.split('-')[-1]
    aidee = int(tail.split('[')[0])
    check = tail.split('[')[1][:-1]
    return name, aidee, check

def checksum(name):
    char_dict = {char: 0 for char in name}
    for char in name:
        char_dict[char] += 1
    realcheck = sorted(char_dict.items(), key=lambda x: (-x[1],x[0]), reverse=False)
    realcheck = ''.join(list(zip(*realcheck))[0][:5])
    return realcheck
    
def is_real(room):
    name, aidee, check = name_id_check(room)
    name = ''.join(name.split('-'))
    if checksum(name) == check:
        return aidee
    else:
        return 0

def sum_real_ids(path):
    with open(path,'rt') as f:
        count = 0
        for line in f:
            room = line.rstrip()
            count += is_real(room)
    return count
sum_real_ids(myinput)<jupyter_output><empty_output><jupyter_text>Challenge 4.2<jupyter_code>def write_real_rooms(inpath, outpath):
    with open(inpath,'rt') as f:
        with open(outpath, 'wt') as g:
            for line in f:
                if is_real(line.rstrip()):
                    g.write(line)
write_real_rooms(myinput, 'inputs/input4-filt.txt')
mynewinput = '/home/fmuinos/projects/adventofcode/2016/ferran/inputs/input4-filt.txt'
! wc -l inputs/input4-filt.txt
alphabet = 'abcdefghijklmnopqrstuvwxyz'
cypher = dict(enumerate(alphabet))
revcypher = dict(map(lambda i:(i[1],i[0]), list(cypher.items())))

def shift_decypher(name, aidee):
    decypher = []
    for char in name:
        if not char == '-':
            new_char = cypher[(revcypher[char] + aidee) % 26]
            decypher.append(new_char)
        else:
            decypher.append(' ')
    return ''.join(decypher)

def write_decyphered_rooms(inpath, outpath):
    with open(inpath,'rt') as f:
        with open(outpath, 'wt') as g:
            for line in f:
                room = line.rstrip()
                name, aidee, check = name_id_check(room)
                g.write(shift_decypher(name, aidee)+' {}'.format(aidee)+'\n')
write_decyphered_rooms(mynewinput,'inputs/input4-decyphered.txt')
! cat inputs/input4-decyphered.txt | grep northpole<jupyter_output>northpole object storage 482
{'fraction_non_alphanumeric': 0.1495601173020528, 'fraction_numerical': 0.016757436112274822, 'mean_word_length': 2.973377703826955, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '277768', 'license': 'MIT', 'n_tokens_mistral': 913, 'n_tokens_neox': 871, 'n_words': 169, 'path': '2016/ferran/day4.ipynb', 'repo_name': 'bbglab/adventofcode', 'size': '2387'}