1
1
from binary_diagnostic import gamma_epsilon_product , gamma_rate , sum_bin_digits
2
- from binary_diagnostic import puzzle_1 , Path
2
+ from binary_diagnostic import puzzle_1 , puzzle_2 , Path
3
+ from binary_diagnostic import valid_reading_functor , filter_readings
4
+ from binary_diagnostic import most_common_digit , least_common_digit
5
+ from binary_diagnostic import find_unique_rating , life_support_rating
3
6
import pytest
4
7
5
8
@@ -13,6 +16,13 @@ def bin_report():
13
16
return ['00100' , '11110' , '10110' , '10111' , '10101' , '01111' , '00111' ,
14
17
'11100' , '10000' , '11001' , '00010' , '01010' ]
15
18
19
+ @pytest .fixture
20
+ def int_report (bin_report ):
21
+ readings = []
22
+ for reading in bin_report :
23
+ readings .append ([int (x ) for x in reading ])
24
+ return readings
25
+
16
26
17
27
def test_gamma_rate (bin_digit_count ):
18
28
assert gamma_rate (bin_digit_count , 12 ) == 22
@@ -21,9 +31,45 @@ def test_gamma_rate(bin_digit_count):
21
31
def test_sum_bin_digits (bin_report , bin_digit_count ):
22
32
assert sum_bin_digits (bin_report ) == bin_digit_count
23
33
34
+
24
35
def test_gamma_epsilon_product (bin_report ):
25
36
assert gamma_epsilon_product (bin_report ) == 198
26
37
27
38
28
39
def test_puzzle_1 ():
29
40
assert puzzle_1 (Path (__file__ ).parent / "puzzle_1_test_input.txt" ) == 198
41
+
42
+
43
+ @pytest .mark .parametrize (("column" , "digit" ), [
44
+ [0 , 1 ], [1 , 0 ], [2 , 1 ], [3 , 1 ], [4 , 1 ]
45
+ ])
46
+ def test_valid_reading_functor (column , digit ):
47
+ assert valid_reading_functor (column , digit )([1 , 0 , 1 , 1 , 1 ])
48
+
49
+
50
+ def test_filter_readings (int_report ):
51
+ filtered_readings = [[1 , 1 , 1 , 1 , 0 ], [1 , 0 , 1 , 1 , 0 ], [1 , 0 , 1 , 1 , 1 ],
52
+ [1 , 0 , 1 , 0 , 1 ], [1 , 1 , 1 , 0 , 0 ], [1 , 0 , 0 , 0 , 0 ],
53
+ [1 , 1 , 0 , 0 , 1 ]]
54
+ assert filter_readings (int_report , 0 , 1 ) == filtered_readings
55
+
56
+
57
+ def test_common_digit_selectors (int_report ):
58
+ assert most_common_digit (report_section = int_report , column = 0 ) == 1
59
+ assert least_common_digit (report_section = int_report , column = 0 ) == 0
60
+ split_report = [[0 ], [0 ], [0 ], [1 ], [1 ], [1 ]]
61
+ assert most_common_digit (report_section = split_report , column = 0 ) == 1
62
+ assert least_common_digit (report_section = split_report , column = 0 ) == 0
63
+
64
+
65
+ def test_find_unique_rating (int_report ):
66
+ assert find_unique_rating (report = int_report , column = 0 , digit_selector = most_common_digit ) == [1 , 0 , 1 , 1 , 1 ]
67
+ assert find_unique_rating (report = int_report , column = 0 , digit_selector = least_common_digit ) == [0 , 1 , 0 , 1 , 0 ]
68
+
69
+
70
+ def test_life_support_rating (int_report ):
71
+ assert life_support_rating (report = int_report ) == 230
72
+
73
+
74
+ def test_puzzle_2 ():
75
+ assert puzzle_2 (Path (__file__ ).parent / "puzzle_1_test_input.txt" ) == 230
0 commit comments