3
3
from collections .abc import Iterable
4
4
5
5
from adventofcode .tooling .coordinates import X , Y
6
- from adventofcode .tooling .map import Map2d
6
+ from adventofcode .tooling .map import IterDirection , Map2d
7
7
8
8
9
9
def _parse_maps (input_str : str ) -> Iterable [Map2d [str ]]:
@@ -31,19 +31,21 @@ def _compare_datas(
31
31
32
32
33
33
def _find_consecutive_rows_or_columns (
34
- map_ : Map2d [str ], start_pos : int , find_column_not_row : bool , allowed_mismatches : int
34
+ map_ : Map2d [str ],
35
+ start_pos : int ,
36
+ direction : IterDirection ,
37
+ allowed_mismatches : int ,
35
38
) -> tuple [int , int ] | None :
36
- if find_column_not_row :
37
- first_y = Y (0 )
38
- first_x = X (start_pos )
39
- else :
40
- first_y = Y (start_pos )
41
- first_x = X (0 )
39
+ match direction :
40
+ case IterDirection .Rows :
41
+ first_y = Y (start_pos )
42
+ first_x = X (0 )
43
+ case IterDirection .Columns :
44
+ first_y = Y (0 )
45
+ first_x = X (start_pos )
42
46
for (i1 , data1 ), (_ , data2 ) in itertools .pairwise (
43
47
(i , [sym for _ , sym in sym_iter ])
44
- for i , sym_iter in map_ .iter_data (
45
- first_y , first_x , columns_first = find_column_not_row
46
- )
48
+ for i , sym_iter in map_ .iter_data (first_y , first_x , direction = direction )
47
49
):
48
50
match_res = _compare_datas (data1 , data2 , allowed_mismatches )
49
51
if match_res is None :
@@ -59,23 +61,24 @@ def _map_data_iter_to_data(d: tuple[int, str]) -> str:
59
61
def _check_if_datas_around_reflection_match (
60
62
map_ : Map2d [str ],
61
63
pos_before_reflection : int ,
62
- find_column_not_row : bool ,
64
+ direction : IterDirection ,
63
65
allowed_mismatches : int ,
64
66
) -> int | None :
65
- if find_column_not_row :
66
- before_first_y = Y (0 )
67
- before_first_x = X (pos_before_reflection - 1 )
68
- before_last_y = map_ .br_y
69
- before_last_x = X (- 1 )
70
- after_first_y = Y (0 )
71
- after_first_x = X (pos_before_reflection + 2 )
72
- else :
73
- before_first_y = Y (pos_before_reflection - 1 )
74
- before_first_x = X (0 )
75
- before_last_y = Y (- 1 )
76
- before_last_x = map_ .br_x
77
- after_first_y = Y (pos_before_reflection + 2 )
78
- after_first_x = X (0 )
67
+ match direction :
68
+ case IterDirection .Rows :
69
+ before_first_y = Y (pos_before_reflection - 1 )
70
+ before_first_x = X (0 )
71
+ before_last_y = Y (- 1 )
72
+ before_last_x = map_ .br_x
73
+ after_first_y = Y (pos_before_reflection + 2 )
74
+ after_first_x = X (0 )
75
+ case IterDirection .Columns :
76
+ before_first_y = Y (0 )
77
+ before_first_x = X (pos_before_reflection - 1 )
78
+ before_last_y = map_ .br_y
79
+ before_last_x = X (- 1 )
80
+ after_first_y = Y (0 )
81
+ after_first_x = X (pos_before_reflection + 2 )
79
82
80
83
mismatches = 0
81
84
@@ -102,7 +105,7 @@ def _check_if_datas_around_reflection_match(
102
105
103
106
104
107
def _find_reflection_line (
105
- map_ : Map2d [str ], find_column_not_row : bool , required_mismatches : int = 0
108
+ map_ : Map2d [str ], direction : IterDirection , required_mismatches : int = 0
106
109
) -> int | None :
107
110
logging .debug (
108
111
"Searching for reflection with %d required mismatches" , required_mismatches
@@ -111,7 +114,7 @@ def _find_reflection_line(
111
114
while True :
112
115
remaining_mismatches = required_mismatches
113
116
found_data_info = _find_consecutive_rows_or_columns (
114
- map_ , search_start_pos , find_column_not_row , remaining_mismatches
117
+ map_ , search_start_pos , direction , remaining_mismatches
115
118
)
116
119
if found_data_info is None :
117
120
logging .debug (
@@ -131,7 +134,7 @@ def _find_reflection_line(
131
134
)
132
135
133
136
check_res = _check_if_datas_around_reflection_match (
134
- map_ , first_pos , find_column_not_row , remaining_mismatches
137
+ map_ , first_pos , direction , remaining_mismatches
135
138
)
136
139
if check_res is None :
137
140
logging .debug (
@@ -159,12 +162,16 @@ def _resolve(input_str: str, required_mismatches_per_map: int) -> int:
159
162
map_ .width ,
160
163
map_ ,
161
164
)
162
- match_index = _find_reflection_line (map_ , False , required_mismatches_per_map )
165
+ match_index = _find_reflection_line (
166
+ map_ , IterDirection .Rows , required_mismatches_per_map
167
+ )
163
168
if match_index is not None :
164
169
line_or_column = "L"
165
170
match_multiplier = 100
166
171
else :
167
- match_index = _find_reflection_line (map_ , True , required_mismatches_per_map )
172
+ match_index = _find_reflection_line (
173
+ map_ , IterDirection .Columns , required_mismatches_per_map
174
+ )
168
175
assert match_index is not None
169
176
line_or_column = "C"
170
177
match_multiplier = 1
0 commit comments