Day 4

We're supposed to figure out how many of these passphrases are composed of unique words

In [1]:
import pandas as pd
import numpy as np

phrases = pd.read_csv("H:/python_jiggering/logs/aoc_2017/d4_input.txt", header = None, sep = '\t')
phrases.head()
Out[1]:
0
0 una bokpr ftz ryw nau yknf fguaczl anu
1 tvay wvco bcoblpt fwzg sfsys zvuqll mcbhwz ovc...
2 ynsocz vid rfmsy essqt fpbjvvq sldje qfpvjvb
3 yvh nxc kla vhy vkbq cxfzgr
4 kljv ymobo jlvk ebst cse cmlub tavputz omoby psif
In [2]:
phrases = phrases[0]
# now write a function to make a judgement:
def all_unique(x):
    x = np.array(x.split())
    if(len(x) == len(np.unique(x))):
        return 1
    else:
        return 0
uniques = phrases.apply(all_unique)
uniques.sum()
Out[2]:
477

Part 2

Now we basically need to do the same thing except we will test for anagrams of the words within phrases as well. This seems like it will be pretty easy to solve if I can figure out a way to sort the letters of a string and then recombine.

Let's try this function to standardize a single word:

In [3]:
def std_word(x):
    letter_list = []
    for let in x:
        letter_list.append(let)
    letter_list.sort()
    out = ''
    for let in letter_list:
        out += let
    return out

Now a function to do this to a whole passphrase

In [4]:
def std_pass(x):
     x = np.array(x.split())
     return ' '.join([std_word(xi) for xi in x])

Finally modify the all_unique function to include this

In [5]:
def all_unique2(x):
    x = np.array(std_pass(x).split())
    if(len(x) == len(np.unique(x))):
        return 1
    else:
        return 0
uniques2 = phrases.apply(all_unique2)
uniques2.sum()
Out[5]:
167