adventofcode 25.8__py3-none-any.whl → 25.9__py3-none-any.whl

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.
adventofcode/__about__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "25.8"
1
+ __version__ = "25.9"
adventofcode/cli.py CHANGED
@@ -66,7 +66,7 @@ def init_templates(year: int, output_dir: Path, num_days: int) -> None:
66
66
  console.log(f"Skipping {filename} (already exists)")
67
67
  continue
68
68
 
69
- filepath.write_text(TEMPLATE.format(day=day, year=year))
69
+ filepath.write_text(TEMPLATE.format(day=day, year=year), encoding="utf-8")
70
70
  console.log(f"Created {filename}")
71
71
 
72
72
  gitignore_path = output_dir / ".gitignore"
@@ -92,7 +92,8 @@ def init_templates(year: int, output_dir: Path, num_days: int) -> None:
92
92
  "# You can find it in your browser's developer tools after logging in to adventofcode.com. \n"
93
93
  "# The name of the cookie is `session`.\n"
94
94
  'AOC_SESSION=""\n'
95
- f"AOC_YEAR={year}\n"
95
+ f"AOC_YEAR={year}\n",
96
+ encoding="utf-8",
96
97
  )
97
98
  console.log("Created .env file (remember to set your AOC_SESSION)")
98
99
 
@@ -231,11 +232,37 @@ def build_markdown_table(results: list[DayResult], total_time: float, total_part
231
232
  return "\n".join(lines)
232
233
 
233
234
 
234
- def update_readme(readme_path: Path, results_table: str) -> None:
235
+ def get_result_row(table: str, pattern: str) -> str | None:
236
+ table_match = re.search(pattern, table, flags=re.MULTILINE)
237
+ return None if table_match is None else table_match.group(0)
238
+
239
+
240
+ def get_time_diff(old_day_row: str, new_day_row: str) -> list[float]:
241
+ pattern = r"(\d+\.\d+)(ms|s)"
242
+
243
+ def parse_time(match: tuple[str, str]) -> float:
244
+ val = float(match[0])
245
+ return val * 1000 if match[1] == "s" else val
246
+
247
+ matches_old = re.findall(pattern, old_day_row)
248
+ matches_new = re.findall(pattern, new_day_row)
249
+
250
+ if len(matches_old) >= 2 and len(matches_new) >= 2:
251
+ part_1_time_old = parse_time(matches_old[0])
252
+ part_2_time_old = parse_time(matches_old[1])
253
+
254
+ part_1_time_new = parse_time(matches_new[0])
255
+ part_2_time_new = parse_time(matches_new[1])
256
+ return [part_1_time_new - part_1_time_old, part_2_time_new - part_2_time_old]
257
+ if len(matches_new) >= 2:
258
+ return [parse_time(matches_new[0]), parse_time(matches_new[1])]
259
+ return [0.0, 0.0]
260
+
261
+
262
+ def update_readme(readme_path: Path, results_table: str, path: Path) -> None:
235
263
  """Update the README.md with benchmark results."""
236
264
  marker_start = "<!-- BENCHMARK_RESULTS_START -->"
237
265
  marker_end = "<!-- BENCHMARK_RESULTS_END -->"
238
-
239
266
  if not readme_path.exists():
240
267
  # Create a new README with results
241
268
  content = dedent("""\
@@ -252,22 +279,73 @@ def update_readme(readme_path: Path, results_table: str) -> None:
252
279
  marker_end=marker_end,
253
280
  results_table=results_table,
254
281
  )
255
- readme_path.write_text(content)
282
+ readme_path.write_text(content, encoding="utf-8")
256
283
  console.log(f"Created {readme_path} with benchmark results")
257
284
  return
258
285
 
259
286
  content = readme_path.read_text()
260
-
261
287
  if marker_start in content and marker_end in content:
262
- # Replace existing results
263
- pattern = re.compile(re.escape(marker_start) + r".*?" + re.escape(marker_end), re.DOTALL)
264
- new_section = f"{marker_start}\n{results_table}\n{marker_end}"
265
- content = pattern.sub(new_section, content)
288
+ if path.is_dir():
289
+ # Replace entire existing results table
290
+ pattern = re.compile(re.escape(marker_start) + r".*?" + re.escape(marker_end), re.DOTALL)
291
+ new_section = f"{marker_start}\n{results_table}\n{marker_end}"
292
+ content = pattern.sub(new_section, content)
293
+ else:
294
+ # strip .py from path
295
+ stem = path.stem
296
+
297
+ # substitute the old day row with the new day row
298
+ day_row_pattern = rf"^(\|\s*{stem}\s*\|).*?(\n)"
299
+ old_day_row = get_result_row(content, day_row_pattern)
300
+ if old_day_row is None:
301
+ console.log(f"[red]Could not extract existing row for path {stem} in README[/red]")
302
+ return
303
+ new_day_row = get_result_row(results_table, day_row_pattern)
304
+ if new_day_row is None:
305
+ console.log(f"[red]Could not find new row for path {stem} in results table[/red]")
306
+ return
307
+ content = re.sub(day_row_pattern, new_day_row, content, flags=re.MULTILINE)
308
+
309
+ # substitute the total day row with new time
310
+ totals_pattern = r"^(\|\s*\*\*Total\*\*\s*\|).*?(\n)"
311
+ old_totals = re.search(totals_pattern, content, flags=re.MULTILINE)
312
+
313
+ time_pattern = r"(\d+\.\d+)(ms|s)"
314
+ matches = re.findall(time_pattern, old_totals.group(0)) if old_totals else []
315
+
316
+ if len(matches) < 3:
317
+ console.log("[red]Could not extract existing totals in README[/red]")
318
+ return
319
+
320
+ def parse_to_ms(val_str: str, unit_str: str) -> float:
321
+ val = float(val_str)
322
+ return val * 1000 if unit_str == "s" else val
323
+
324
+ current_p1_ms = parse_to_ms(matches[0][0], matches[0][1])
325
+ current_p2_ms = parse_to_ms(matches[1][0], matches[1][1])
326
+ current_total_ms = parse_to_ms(matches[2][0], matches[2][1])
327
+
328
+ [part_1_diff_ms, part_2_diff_ms] = get_time_diff(old_day_row, new_day_row)
329
+
330
+ new_p1_ms = current_p1_ms + part_1_diff_ms
331
+ new_p2_ms = current_p2_ms + part_2_diff_ms
332
+ new_total_ms = current_total_ms + part_1_diff_ms + part_2_diff_ms
333
+
334
+ p1_seconds = new_p1_ms / 1000.0
335
+ p2_seconds = new_p2_ms / 1000.0
336
+ total_seconds = new_total_ms / 1000.0
337
+
338
+ p1_str = markdown_color(format_time(p1_seconds), time_to_color(p1_seconds))
339
+ p2_str = markdown_color(format_time(p2_seconds), time_to_color(p2_seconds))
340
+ total_str = markdown_color(format_time(total_seconds), time_to_color(total_seconds))
341
+
342
+ new_totals = f"| **Total** | | {p1_str} | {p2_str} | {total_str} |\n"
343
+ content = re.sub(totals_pattern, new_totals, content, flags=re.MULTILINE)
266
344
  else:
267
345
  # Append results section
268
346
  content += f"\n## Benchmark Results\n\n{marker_start}\n{results_table}\n{marker_end}\n"
269
347
 
270
- readme_path.write_text(content)
348
+ readme_path.write_text(content, encoding="utf-8")
271
349
  console.log(f"Updated {readme_path} with benchmark results")
272
350
 
273
351
 
@@ -316,11 +394,12 @@ def build_console_table(
316
394
  return table
317
395
 
318
396
 
319
- def benchmark(directory: Path) -> None:
397
+ def benchmark(path: Path) -> None:
320
398
  console.log("[bold]Running benchmarks for Advent of Code[/bold]\n")
321
-
322
- # Find all day files
323
- day_files = sorted(directory.glob("[0-9][0-9]*.py"))
399
+ if path.is_dir():
400
+ day_files = sorted(path.glob("[0-9][0-9]*.py"))
401
+ else:
402
+ day_files = [Path(path)] if path.exists() else []
324
403
  if not day_files:
325
404
  console.log("[yellow]No day files found[/yellow]")
326
405
  return
@@ -368,11 +447,14 @@ def benchmark(directory: Path) -> None:
368
447
  )
369
448
  )
370
449
 
371
- readme_path = directory / "README.md"
450
+ if path.is_dir():
451
+ readme_path = path / "README.md"
452
+ else:
453
+ readme_path = path.parent / "README.md"
372
454
  results_table = build_markdown_table(
373
455
  results, total_time=total_time, total_part_1=total_part_1, total_part_2=total_part_2
374
456
  )
375
- update_readme(readme_path, results_table)
457
+ update_readme(readme_path, results_table, path)
376
458
 
377
459
 
378
460
  def run(filepath: Path, benchmark: bool = False) -> None: # noqa: FBT001, FBT002
@@ -469,8 +551,9 @@ def main() -> None:
469
551
  formatter_class=argparse.RawDescriptionHelpFormatter,
470
552
  epilog=dedent("""\
471
553
  Examples:
472
- adventofcode benchmark # Run benchmarks in current directory
473
- adventofcode benchmark ./2024 # Run benchmarks in the ./2024 directory
554
+ adventofcode benchmark # Run benchmarks in current directory
555
+ adventofcode benchmark -d ./2024 # Run benchmarks in the ./2024 directory
556
+ adventofcode benchmark -d ./2024 2 # Run benchmarks in the ./2024 directory for day 2 only
474
557
 
475
558
  The benchmark will:
476
559
  1. Find all day files (01.py, 02.py, etc.)
@@ -485,11 +568,11 @@ def main() -> None:
485
568
  """),
486
569
  )
487
570
  benchmark_parser.add_argument(
488
- "directory",
571
+ "path",
489
572
  type=Path,
490
573
  nargs="?",
491
574
  default=Path("."),
492
- help="Directory containing day files (default: current directory)",
575
+ help="Path where benchmarked file(s) are located (default: current directory)",
493
576
  )
494
577
 
495
578
  # Run Command
@@ -522,7 +605,7 @@ def main() -> None:
522
605
  case "init":
523
606
  init_templates(args.year, args.directory, 12 if args.year >= 2025 else 25)
524
607
  case "benchmark":
525
- benchmark(args.directory)
608
+ benchmark(args.path)
526
609
  case "run":
527
610
  run(args.filepath, benchmark=args.benchmark)
528
611
  case _:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: adventofcode
3
- Version: 25.8
3
+ Version: 25.9
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
@@ -163,9 +163,13 @@ uv run adventofcode benchmark
163
163
 
164
164
  This will run each day's solution and parse the timing results from the output. The results will be printed to the console as well as added to your README.md.
165
165
 
166
- Example console output:
166
+ If you are in a hurry you can also benchmark a single day's solution with
167
167
 
168
+ ```shell
169
+ uv run adventofcode benchmark 05.py
170
+ ```
168
171
 
172
+ Example console output:
169
173
 
170
174
  Example README:
171
175
 
@@ -0,0 +1,9 @@
1
+ adventofcode/__about__.py,sha256=4A9XMPg86X1WS9nqabg9JCJF6NklftkWEe4gQfoHIek,21
2
+ adventofcode/__init__.py,sha256=JRWxftg5aZS5lArASJk9icNYIIaqgAOQ_bZO4hzmT1E,6172
3
+ adventofcode/cli.py,sha256=CcQuKB07PfoQr6fYtIfHchV6MPJsFU6D1md0knFuzVk,21316
4
+ adventofcode/utils.py,sha256=QJ2AanjoNt_yVI8VZM9plrl9cpHsEhl29NyA4Aqs8Ug,258
5
+ adventofcode-25.9.dist-info/METADATA,sha256=3ZLhEnexvPWQ-EK5yoniAfyvPT9x-jCiVfeSqFkhJes,7772
6
+ adventofcode-25.9.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
7
+ adventofcode-25.9.dist-info/entry_points.txt,sha256=Yw9pyDrVVS3XWRcjOv1BnAxkqtJdYoG747A1LysScak,83
8
+ adventofcode-25.9.dist-info/licenses/LICENSE,sha256=7w16SgIHVGYHh8TS_-XnKFvY6aKa_JDtVFoduWtIPTY,1069
9
+ adventofcode-25.9.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- adventofcode/__about__.py,sha256=OUcz3af8HsYu9qt6dxP07ZestY2_JQ9SAbshAKQEvms,21
2
- adventofcode/__init__.py,sha256=JRWxftg5aZS5lArASJk9icNYIIaqgAOQ_bZO4hzmT1E,6172
3
- adventofcode/cli.py,sha256=byyhvsnt93pbXgRJe9l7JtpxvQXVQdaua7QcdsclZZc,17414
4
- adventofcode/utils.py,sha256=QJ2AanjoNt_yVI8VZM9plrl9cpHsEhl29NyA4Aqs8Ug,258
5
- adventofcode-25.8.dist-info/METADATA,sha256=QlUgPTUPUaLWEUbk22_xugQqfJ_3il58G0Qvve9Sj9k,7649
6
- adventofcode-25.8.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
7
- adventofcode-25.8.dist-info/entry_points.txt,sha256=Yw9pyDrVVS3XWRcjOv1BnAxkqtJdYoG747A1LysScak,83
8
- adventofcode-25.8.dist-info/licenses/LICENSE,sha256=7w16SgIHVGYHh8TS_-XnKFvY6aKa_JDtVFoduWtIPTY,1069
9
- adventofcode-25.8.dist-info/RECORD,,