Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a solutions column #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ Should be some kind of Markdown header level.

Location of the README file to edit.

### `solutionLocations`

*Optional* - default is none

Location of the solution files relative to the `readmeLocation`,
the following placeholders can be used:

- `{d}` The day of the solution (e.g. 3, 5 and 10)
- `{dd}` The padded day of the solution (e.g. 03, 05 and 10)
- `{yyyy}` The year of the solution (e.g. 2021 or 2023)
- `{yy}` The truncated year of the solution (e.g. 21 or 23)

## Like this project?

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/purple_img.png)](https://www.buymeacoffee.com/k2bd)
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ inputs:
description: Location of the README file
required: false
default: "README.md"
solutionLocations:
description: Location of your solution for each day
required: false
default: ""
branding:
icon: "star"
color: "purple"
2 changes: 2 additions & 0 deletions src/advent_readme_stars/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@

#: Stars info endpoint
STARS_ENDPOINT = f"{ADVENT_URL}/{YEAR}/leaderboard/private/view/{LEADERBOARD_ID}.json"

SOLUTION_LOCATIONS = os.environ.get("INPUT_SOLUTIONLOCATIONS", "")
61 changes: 58 additions & 3 deletions src/advent_readme_stars/update.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os.path
from typing import List

from advent_readme_stars.constants import (
ADVENT_URL,
HEADER_PREFIX,
README_LOCATION,
SOLUTION_LOCATIONS,
STAR_SYMBOL,
TABLE_MARKER,
YEAR,
Expand Down Expand Up @@ -62,11 +65,63 @@ def insert_table(lines: List[str]) -> List[str]:
return lines[:table_location] + to_insert + lines[table_location:]


def insert_solutions_table(lines: List[str]) -> List[str]:
"""
Search the lines for a table marker,
and insert a table with links to the solutions there.
"""
table_location = None
for i, line in enumerate(lines):
if line.strip() == TABLE_MARKER:
table_location = i
break
else:
return lines

to_insert = [
TABLE_MARKER,
f"{HEADER_PREFIX} {YEAR} Results",
"",
"| Day | Solution | Part 1 | Part 2 |",
"| :---: | :---: | :---: | :---: |",
]
stars_info = sorted(list(get_progress()), key=lambda p: p.day)

for star_info in stars_info:
day_url = f"{ADVENT_URL}/{YEAR}/day/{star_info.day}"
day_text = f"[Day {star_info.day}]({day_url})"
part_1_text = STAR_SYMBOL if star_info.part_1 else " "
part_2_text = STAR_SYMBOL if star_info.part_2 else " "
solution = get_solution(YEAR, star_info.day)
to_insert.append(f"| {day_text} | {solution} | {part_1_text} | {part_2_text} |")

return lines[:table_location] + to_insert + lines[table_location:]


def get_solution(year: int, day: int) -> str:
"""
Gets the solution text for a specific day
"""
solution_rel_path = (SOLUTION_LOCATIONS
.replace("{d}", str(day))
.replace("{dd}", str(day).rjust(2, "0"))
.replace("{yyyy}", str(year))
.replace("{yy}", str(year)[-2:]))
readme_dir = README_LOCATION.removesuffix(os.path.basename(README_LOCATION))
solution_location = os.path.join(readme_dir, solution_rel_path)
if os.path.exists(solution_location):
file_name = os.path.basename(solution_location)
return f"[{file_name}]({solution_rel_path})"
else:
return ""


def update_readme(readme: List[str]) -> List[str]:
"""
Take the contents of a readme file and update them
"""
reduced = remove_existing_table(readme)
new_readme = insert_table(reduced)

return new_readme
if SOLUTION_LOCATIONS != "":
return insert_solutions_table(reduced)
else:
return insert_table(reduced)