-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.py
92 lines (63 loc) · 2.56 KB
/
4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import unittest
from itertools import permutations as perms
"""
--- Day 4: High-Entropy Passphrases ---
A new system policy has been put in place that requires all accounts to use a
passphrase instead of simply a password. A passphrase consists of a series of
words (lowercase letters) separated by spaces.
To ensure security, a valid passphrase must contain no duplicate words.
For example:
aa bb cc dd ee is valid.
aa bb cc dd aa is not valid - the word aa appears more than once.
aa bb cc dd aaa is valid - aa and aaa count as different words.
The system's full passphrase list is available as your puzzle input. How many
passphrases are valid?
"""
def all_unique_no_perms(line):
""" returns True if line contains all unique words, False if not """
if all_unique(line) == False:
return False
for word in line:
p = [''.join(x) for x in perms(word)]
for pp in p:
if pp != word and pp in line:
return False
return True
def all_unique(line):
""" returns True if line contains all unique words, False if not """
return len(list(line)) == len(set(line))
def list_lines(file_object):
return map(split_strip, file_object)
def split_strip(line):
""" map a line from a file object to a list of 'words' split on (not
necessarily uniform!) spaces """
return list(filter(lambda x: x != '', line.strip().split(' ')))
def valid_passphrases(f):
""" return a sum of all unique words in a file object f """
lines = f.readlines()
xs = list(filter(all_unique, list_lines(lines)))
f.close()
return len(xs)
def valid_passphrases_no_perms(f):
""" return a sum of all lines that contain no words that could be a perm in a file object f """
lines = f.readlines()
xs = list(filter(all_unique_no_perms, list_lines(lines)))
f.close()
print('# of lines {0} and valid {1}'.format(len(lines), len(xs)))
return len(xs)
f = open('./input/4.txt')
v = valid_passphrases_no_perms(f)
print('Result: {}'.format(v))
# Tests
class TestCountUniqueWords(unittest.TestCase):
def test_should_count_all_when_every_line_is_unique(self):
mock_2 = open('./test/4_0mock.txt')
self.assertEqual(valid_passphrases(mock_2), 6)
def test_only_counts_one_for_lines_with_filled_repeats(self):
mock_1 = open('./test/4_1mock.txt')
self.assertEqual(valid_passphrases(mock_1), 3)
def test_only_counts_unique_words(self):
mock = open('./test/4_2mock.txt')
self.assertEqual(valid_passphrases(mock), 2)
if __name__ == '__main__':
unittest.main()