High Entropy Passphrases — Python — #adventofcode Day 4

Series: Advent of Code 2017

Today’s challenge describes some simple rules supposedly intended to enforce the use of secure passwords. All we have to do is test a list of passphrase and identify which ones meet the rules.

→ Full code on GitHub

!!! commentary Fearing that today might be as time-consuming as yesterday, I returned to Python and it’s hugely powerful “batteries-included” standard library. Thankfully this challenge was more straightforward, and I actually finished this before finishing day 3.

First, let’s import two useful utilities.

from fileinput import input
from collections import Counter

Part 1 requires simply that a passphrase contains no repeated words. No problem: we split the passphrase into words and count them, and check if any was present more than once.

Counter is an amazingly useful class to have in a language’s standard library. All it does is count things: you add objects to it, and then it will tell you how many of a given object you have. We’re going to use it to count those potentially duplicated words.

def is_valid(passphrase):
    counter = Counter(passphrase.split())
    return counter.most_common(1)[0][1] == 1

Part 2 requires that no word in the passphrase be an anagram of any other word. Since we don’t need to do anything else with the words afterwards, we can check for anagrams by sorting the letters in each word: “leaf” and “flea” both become “aefl” and can be compared directly. Then we count as before.

def is_valid_ana(passphrase):
    counter = Counter(''.join(sorted(word)) for word in passphrase.split())
    return counter.most_common(1)[0][1] == 1

Finally we pull everything together. sum(map(boolean_func, list)) is a common idiom in Python for counting the number of times a condition (checked by boolean_func) is true. In Python, True and False can be treated as the numbers 1 and 0 respectively, so that summing a list of Boolean values gives you the number of True values in the list.

lines = list(input())
print(sum(map(is_valid, lines)))
print(sum(map(is_valid_ana, lines)))

Webmentions

You can respond to this post, "High Entropy Passphrases — Python — #adventofcode Day 4", by: liking, boosting or replying to a tweet or toot that mentions it; or sending a webmention from your own site to https://erambler.co.uk/blog/day-04/

Comments & reactions haven't loaded yet. You might have JavaScript disabled but that's cool 😎.

Comments

Powered by Cactus Comments 🌵