-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_4.py
83 lines (68 loc) · 2.39 KB
/
day_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
from __future__ import division, print_function
import os
from my_utils.tests import test_function
def part_1(passphrases):
"""Function which calculates the solution to part 1
Arguments
---------
Returns
-------
"""
valid = []
for pp in passphrases:
pp_list = pp.split()
pp_set = set(pp_list)
if len(pp_list) != len(pp_set):
valid += [0]
else:
valid += [1]
return sum(valid)
def part_2(passphrases):
"""Function which calculates the solution to part 2
Arguments
---------
Returns
-------
"""
pp_list = [pp.split() for pp in passphrases]
pp_sorted_words = [[''.join(sorted(word)) for word in pp]
for pp in pp_list]
pp_sorted_words = [' '.join(pp) for pp in pp_sorted_words]
return part_1(pp_sorted_words)
def main(test_datas, functions, puzzle_input=None):
for ii, (test_data, fun) in enumerate(zip(test_datas, functions)):
nr_errors = test_function(fun, test_data)
if nr_errors == 0:
print('Pt. {} Tests Passed'.format(ii+1))
if puzzle_input is not None:
fn = os.path.basename(__file__)
for ii, fun in enumerate(functions):
ans = fun(puzzle_input)
print('{} Pt. {} Solution: {}'.format(fn, ii+1, ans))
if __name__ == "__main__":
# Testing data:
# - each element of input list will be passed to function
# - the relative element in output list is the expected output
test_data1 = {
'inputs': [['aa bb cc dd ee'],
['aa bb cc dd aa'],
['aa bb cc dd aaa']],
'outputs': [1, 0, 1]
}
test_data2 = {
'inputs': [['abcde fghij'],
['abcde xyz ecdab'],
['a ab abc abd abf abj'],
['iiii oiii ooii oooi oooo'],
['oiii ioii iioi iiio'],
['oiii ioii iioi iiio iiii iiiii'],
['abcde fghij', 'abcde fghkj']],
'outputs': [1, 0, 1, 1, 0, 0, 2]
}
# Code to import the actual puzzle input
with open('./inputs/day_4.txt') as f:
puzzle_input = [line.rstrip('\n') for line in f]
# Main call: performs testing and calculates puzzle outputs
main(test_datas=[test_data1, test_data2],
functions=[part_1, part_2],
puzzle_input=puzzle_input)