adventofcode 25.7__tar.gz → 25.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Calendar Versioning](https://calver.org).
7
7
 
8
+ ## [25.8]
9
+
10
+ ### Added
11
+
12
+ * Ability to benchmark different solution for one day. Requested by @rodrigogiraoserrao ❤️
13
+ * More stable benchmarking (using [Timer.autorange](https://docs.python.org/3/library/timeit.html#timeit.Timer.autorange))
14
+
15
+ ### Fixed
16
+
17
+ * Improved the README.md template
8
18
 
9
19
  ## [25.7]
10
20
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: adventofcode
3
- Version: 25.7
3
+ Version: 25.8
4
4
  Summary: Helper utilities for solving Advent of Code puzzles
5
5
  Project-URL: Documentation, https://github.com/anze3db/adventofcode#readme
6
6
  Project-URL: Issues, https://github.com/anze3db/adventofcode/issues
@@ -0,0 +1 @@
1
+ __version__ = "25.8"
@@ -6,6 +6,7 @@ import time
6
6
  from collections.abc import Callable
7
7
  from contextlib import contextmanager
8
8
  from pathlib import Path
9
+ from timeit import Timer
9
10
  from typing import Any
10
11
 
11
12
  import percache
@@ -129,6 +130,18 @@ class AoC:
129
130
  perf.value = answer
130
131
  submit(year=self.year, day=self.day, level=2, answer=answer)
131
132
 
133
+ def submit_p1_benchmark(self):
134
+ inp = self.get_input()
135
+ num_loops, total_time = Timer(lambda: self.part_1(inp)).autorange()
136
+ console.log(f"{format_time(total_time / num_loops)} to {PART_1_RETURN_STR}")
137
+ submit(year=self.year, day=self.day, level=1, answer=self.part_1(inp))
138
+
139
+ def submit_p2_benchmark(self):
140
+ inp = self.get_input()
141
+ num_loops, total_time = Timer(lambda: self.part_2(inp)).autorange()
142
+ console.log(f"{format_time(total_time / num_loops)} to {PART_2_RETURN_STR}")
143
+ submit(year=self.year, day=self.day, level=2, answer=self.part_2(inp))
144
+
132
145
 
133
146
  @cache
134
147
  def get_puzzle(day: int, year: int, part: int) -> str:
@@ -148,14 +148,15 @@ def time_to_color(seconds: float) -> Color:
148
148
 
149
149
 
150
150
  def run_day(filepath: Path) -> DayResult:
151
- day_num = int(re.sub(r"[^0-9]", "", filepath.stem))
151
+ match = re.match(r"(\d\d)", filepath.stem)
152
+ day_num = int(match.group(1)) if match else 0
152
153
  if datetime.date(AOC_YEAR, 12, day_num) > today_est:
153
154
  return DayResult(day=filepath.stem, status="🕑")
154
155
 
155
156
  result = DayResult(day=filepath.stem)
156
157
 
157
158
  if "adventofcode run" in filepath.read_text():
158
- command = ["adventofcode", "run", str(filepath)]
159
+ command = ["adventofcode", "run", str(filepath), "--benchmark"]
159
160
  else:
160
161
  command = [sys.executable, str(filepath)]
161
162
  try:
@@ -237,7 +238,20 @@ def update_readme(readme_path: Path, results_table: str) -> None:
237
238
 
238
239
  if not readme_path.exists():
239
240
  # Create a new README with results
240
- content = f"# Advent of Code\n\n## Benchmark Results\n\n{marker_start}\n{results_table}\n{marker_end}\n"
241
+ content = dedent("""\
242
+ # Advent of Code {AOC_YEAR} 🎄
243
+
244
+ ## Solution Benchmarks
245
+
246
+ {marker_start}
247
+ {results_table}
248
+ {marker_end}
249
+ """).format(
250
+ AOC_YEAR=AOC_YEAR,
251
+ marker_start=marker_start,
252
+ marker_end=marker_end,
253
+ results_table=results_table,
254
+ )
241
255
  readme_path.write_text(content)
242
256
  console.log(f"Created {readme_path} with benchmark results")
243
257
  return
@@ -275,7 +289,7 @@ def markdown_color(formatted_time: str, color: Color) -> str:
275
289
  def build_console_table(
276
290
  results: list[DayResult], current_running: str | None, total_part_1: float, total_part_2: float, total_time: float
277
291
  ) -> Table:
278
- table = Table(title="Advent of Code Benchmarks")
292
+ table = Table(title=f"Advent of Code {AOC_YEAR} Benchmark")
279
293
  table.add_column("Day", style="cyan", justify="right")
280
294
  table.add_column("Status", justify="center")
281
295
  table.add_column("Part 1", justify="right")
@@ -306,7 +320,7 @@ def benchmark(directory: Path) -> None:
306
320
  console.log("[bold]Running benchmarks for Advent of Code[/bold]\n")
307
321
 
308
322
  # Find all day files
309
- day_files = sorted(directory.glob("[0-9][0-9].py"))
323
+ day_files = sorted(directory.glob("[0-9][0-9]*.py"))
310
324
  if not day_files:
311
325
  console.log("[yellow]No day files found[/yellow]")
312
326
  return
@@ -361,7 +375,7 @@ def benchmark(directory: Path) -> None:
361
375
  update_readme(readme_path, results_table)
362
376
 
363
377
 
364
- def run(filepath: Path) -> None:
378
+ def run(filepath: Path, benchmark: bool = False) -> None: # noqa: FBT001, FBT002
365
379
  # Import the python module on path
366
380
  if not filepath.exists():
367
381
  console.log(f"[red]File {filepath} does not exist[/red]")
@@ -375,21 +389,37 @@ def run(filepath: Path) -> None:
375
389
  module = importlib.util.module_from_spec(spec)
376
390
  spec.loader.exec_module(module)
377
391
 
378
- aoc = AoC(part_1=module.part1, part_2=module.part2, day=int(filepath.stem), year=AOC_YEAR)
392
+ # Match the first two digits and use them as the day number
393
+ # Assuming the files are named like 01.py, 02-alternative.py, 02-alternative2.py, etc.
394
+ match = re.match(r"(\d\d)", filepath.stem)
395
+ if not match:
396
+ return
397
+ aoc = AoC(
398
+ part_1=module.part1,
399
+ part_2=module.part2,
400
+ day=int(match.group(1)),
401
+ year=AOC_YEAR,
402
+ )
379
403
  if hasattr(module, "part1_asserts"):
380
404
  for test_input, expected in module.part1_asserts:
381
405
  aoc.assert_p1(test_input, expected)
382
406
  else:
383
407
  console.log("[yellow]No part 1 assertions found[/yellow]\n")
384
408
 
385
- aoc.submit_p1()
409
+ if benchmark:
410
+ aoc.submit_p1_benchmark()
411
+ else:
412
+ aoc.submit_p1()
386
413
  if hasattr(module, "part2_asserts"):
387
414
  for test_input, expected in module.part2_asserts:
388
415
  aoc.assert_p2(test_input, expected)
389
416
  else:
390
417
  console.log("[yellow]No part 2 assertions found[/yellow]\n")
391
418
 
392
- aoc.submit_p2()
419
+ if benchmark:
420
+ aoc.submit_p2_benchmark()
421
+ else:
422
+ aoc.submit_p2()
393
423
 
394
424
 
395
425
  def main() -> None:
@@ -473,6 +503,13 @@ def main() -> None:
473
503
  adventofcode run 01.py # Run day 1 solution in current directory
474
504
  """),
475
505
  )
506
+ run_parser.add_argument(
507
+ "-b",
508
+ "--benchmark",
509
+ action="store_true",
510
+ help="Benchmark each part with Time.autorange()",
511
+ default=False,
512
+ )
476
513
  run_parser.add_argument(
477
514
  "filepath",
478
515
  type=Path,
@@ -487,7 +524,7 @@ def main() -> None:
487
524
  case "benchmark":
488
525
  benchmark(args.directory)
489
526
  case "run":
490
- run(args.filepath)
527
+ run(args.filepath, benchmark=args.benchmark)
491
528
  case _:
492
529
  parser.print_help()
493
530
 
@@ -1 +0,0 @@
1
- __version__ = "25.7"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes