Attempt to solve a sequence of grids. Report results. When showif is a number of seconds, display puzzles that take longer. When showif is None, don't display any puzzles.
(grids, name="", showif=0.0)
| 167 | |
| 168 | |
| 169 | def solve_all(grids, name="", showif=0.0): |
| 170 | """ |
| 171 | Attempt to solve a sequence of grids. Report results. |
| 172 | When showif is a number of seconds, display puzzles that take longer. |
| 173 | When showif is None, don't display any puzzles. |
| 174 | """ |
| 175 | |
| 176 | def time_solve(grid): |
| 177 | start = time.monotonic() |
| 178 | values = solve(grid) |
| 179 | t = time.monotonic() - start |
| 180 | ## Display puzzles that take long enough |
| 181 | if showif is not None and t > showif: |
| 182 | display(grid_values(grid)) |
| 183 | if values: |
| 184 | display(values) |
| 185 | print(f"({t:.5f} seconds)\n") |
| 186 | return (t, solved(values)) |
| 187 | |
| 188 | times, results = zip(*[time_solve(grid) for grid in grids]) |
| 189 | if (n := len(grids)) > 1: |
| 190 | print( |
| 191 | "Solved %d of %d %s puzzles (avg %.2f secs (%d Hz), max %.2f secs)." # noqa: UP031 |
| 192 | % (sum(results), n, name, sum(times) / n, n / sum(times), max(times)) |
| 193 | ) |
| 194 | |
| 195 | |
| 196 | def solved(values): |
no test coverage detected