High Entropy Passphrases — Python — #adventofcode Day 4
Series
This post is part of the series Advent of Code 2017
- Reflections on #aoc2017
- The Halting Problem — Python — #adventofcode Day 25
- Electromagnetic Moat — Rust — #adventofcode Day 24
- Coprocessor Conflagration — Haskell — #adventofcode Day 23
- Sporifica Virus — Rust — #adventofcode Day 22
- Fractal Art — Python — #adventofcode Day 21
- Particle Swarm — Python — #adventofcode Day 20
- A Series of Tubes — Rust — #adventofcode Day 19
- Duet — Haskell — #adventofcode Day 18
- Spinlock — Rust/Python — #adventofcode Day 17
- Permutation Promenade — Julia — #adventofcode Day 16
- Dueling Generators — Rust — #adventofcode Day 15
- Disk Defragmentation — Haskell — #adventofcode Day 14
- Packet Scanners — Haskell — #adventofcode Day 13
- Digital Plumber — Python — #adventofcode Day 12
- Hex Ed — Python — #adventofcode Day 11
- Knot Hash — Haskell — #adventofcode Day 10
- Stream Processing — Haskell — #adventofcode Day 9
- I Heard You Like Registers — Python — #adventofcode Day 8
- Recursive Circus — Ruby — #adventofcode Day 7
- Memory Reallocation — Python — #adventofcode Day 6
- A Maze of Twisty Trampolines — C++ — #adventofcode Day 5
- > High Entropy Passphrases — Python — #adventofcode Day 4 <
- Spiral Memory — Go — #adventofcode Day 3
- Corruption Checksum — Python — #adventofcode Day 2
- Inverse Captcha — Coconut — #adventofcode Day 1
- Advent of Code 2017: introduction
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.
!!! 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
Powered by Cactus Comments 🌵