Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """Day 4: check for valid passwords: no duplicate words """
- from collections import Counter
- # Input file
- with open('Day4input.txt', 'r') as f:
- # Parse lines into lists of separate strings
- passphrases = [s.strip().split() for s in f.read().splitlines()]
- def valid(counter):
- """Test if passphrase has repeated words."""
- if any(x > 1 for x in counter.values()):
- return 0
- return 1
- # Part 1
- total = 0
- for p in passphrases:
- total += valid(Counter(p))
- print(total)
- # Part 2
- total2 = 0
- for p in passphrases:
- # sort the words in the passphrase alphabetically to make Counting possible
- sorted_list = list(map(lambda x: ''.join(sorted(x)), p))
- c = Counter(sorted_list)
- total2 += valid(c)
- print(total2)
Advertisement
Add Comment
Please, Sign In to add comment