@@ -89,6 +89,22 @@ def input_data(self, part: int) -> str | None:
8989 return None
9090 return data_path .read_text ().rstrip ()
9191
92+ def compare (self , want , data : str , parser : typing .Callable , msg_success : str , msg_fail : str , func : typing .Callable , ** kwargs ):
93+ if parser :
94+ data = parser (data )
95+ time_s , got = helpers .timed (func , data = data , ** kwargs )
96+ if str (got ) == str (want ):
97+ print (msg_success % time_s )
98+ else :
99+ msg = msg_fail % (time_s , got )
100+ if str (want ).isdigit () and str (got ).isdigit ():
101+ delta = int (want ) - int (got )
102+ if delta > 0 :
103+ msg += f" Too low by { delta } ."
104+ else :
105+ msg += f" Too high by { - delta } ."
106+ print (msg )
107+
92108 def run_day (self , check : bool , solve : bool , test : bool , formatter ) -> None :
93109 solution_file = pathlib .Path (f"{ self .year } /{ self .module_name ()} .py" )
94110 if not solution_file .exists ():
@@ -100,15 +116,15 @@ def run_day(self, check: bool, solve: bool, test: bool, formatter) -> None:
100116 for part in self .parts :
101117 formatter .set_part (part )
102118 for test_number , (test_part , test_data , test_want ) in enumerate (module .TESTS , 1 ):
103- if parser :
104- test_data = parser (test_data )
105119 if test_part != part :
106120 continue
107- time_s , got = helpers .timed (module .solve , part = part , data = test_data , testing = True , test_number = test_number )
108- if got == test_want :
109- print (f"TEST { self .day :02} .{ part } { time_s } PASS (test { test_number } )" )
110- else :
111- print (f"TEST { self .day :02} .{ part } { time_s } FAIL (test { test_number } ). Got { got } but wants { test_want } ." )
121+ self .compare (
122+ test_want , test_data , parser ,
123+ f"TEST { self .day :02} .{ part } %s PASS (test { test_number } )" ,
124+ f"TEST { self .day :02} .{ part } %s FAIL (test { test_number } ). Got %r but wants { test_want !r} ." ,
125+ module .solve ,
126+ part = part , testing = True , test_number = test_number ,
127+ )
112128 if solve :
113129 for part in self .parts :
114130 formatter .set_part (part )
@@ -131,14 +147,13 @@ def run_day(self, check: bool, solve: bool, test: bool, formatter) -> None:
131147 if data is None :
132148 print (f"CHECK No input data found for day { day } part { part } " )
133149 continue
134- if parser :
135- data = parser (data )
136- time_s , got = helpers .timed (module .solve , part = part , data = data , testing = False , test_number = None )
137- if str (got ) == want [part - 1 ]:
138- print (f"CHECK { self .day :02} .{ part } { time_s } PASS" )
139- else :
140- print (f"CHECK { self .day :02} .{ part } { time_s } FAIL. Wanted { want [part - 1 ]} but got { got } ." )
141-
150+ self .compare (
151+ want [part - 1 ], data , parser ,
152+ f"CHECK { self .day :02} .{ part } %s PASS" ,
153+ f"CHECK { self .day :02} .{ part } %s FAIL. Wanted { want [part - 1 ]} but got %s." ,
154+ module .solve ,
155+ part = part , testing = False , test_number = None ,
156+ )
142157
143158 def run (self , check : bool , solve : bool , test : bool , live : bool ) -> None :
144159 formatter = helpers .setup_logging (self .day , self .verbose )
0 commit comments