Solution to problem 4 in Python
This commit is contained in:
parent
ee5f84f2e6
commit
cf65bb63fc
@ -29,5 +29,38 @@ def part_1() -> None:
|
||||
print(f"There are {valid} valid passphrases")
|
||||
|
||||
|
||||
# --- Part Two ---
|
||||
|
||||
# For added security, yet another system policy has been put in place. Now, a
|
||||
# valid passphrase must contain no two words that are anagrams of each other -
|
||||
# that is, a passphrase is invalid if any word's letters can be rearranged to
|
||||
# form any other word in the passphrase.
|
||||
|
||||
# For example:
|
||||
|
||||
# abcde fghij is a valid passphrase.
|
||||
# abcde xyz ecdab is not valid - the letters from the third word can be
|
||||
# rearranged to form the first word.
|
||||
# a ab abc abd abf abj is a valid passphrase, because all letters need to
|
||||
# be used when forming another word.
|
||||
# iiii oiii ooii oooi oooo is valid.
|
||||
# oiii ioii iioi iiio is not valid - any of these words can be rearranged
|
||||
# to form any other word.
|
||||
|
||||
# Under this new system policy, how many passphrases are valid?
|
||||
|
||||
|
||||
def part_2() -> None:
|
||||
valid = sum(
|
||||
1
|
||||
for passphrase in passphrase_list
|
||||
if len(["".join(sorted(word)) for word in passphrase.split()])
|
||||
== len(set(["".join(sorted(word)) for word in passphrase.split()]))
|
||||
)
|
||||
|
||||
print(f"There are {valid} valid passphrases using the new policy")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part_1()
|
||||
part_2()
|
||||
|
Loading…
Reference in New Issue
Block a user