aoc-automation
Version:
Advent of Code tool to automate the repetitive parts of AoC.
126 lines (95 loc) • 2.97 kB
text/typescript
import type { YearConfig } from "../types/common";
import { stripIndents } from "common-tags";
import toFixed from "../utils/toFixed.js";
const renderYearDayBadges = (config: YearConfig) => {
return config.days
.map(({ part1, part2 }, index) => {
const day = String(index + 1).padStart(2, "0");
const color =
(part1.solved && part2.solved) || (part1.solved && day === "25")
? "green"
: part1.solved || part2.solved
? "yellow"
: "gray";
const badge = ` ? 5 : 6
}/${color})`;
return color !== "gray" ? `[${badge}](day${day})` : badge;
})
.join("\n");
};
const renderYearResults = (config: YearConfig) => {
let totalTime = 0;
let totalStars = 0;
const results = config.days
.map(({ title, part1, part2 }, index) => {
const day = String(index + 1).padStart(2, "0");
let timeBoth = 0;
if (part1.solved) {
totalStars++;
totalTime += part1.time ?? 0;
timeBoth += part1.time ?? 0;
}
if (part2.solved) {
totalStars++;
totalTime += part2.time ?? 0;
timeBoth += part2.time ?? 0;
}
if (day === "25" && part1.solved) {
totalStars++;
}
return stripIndents`
\`\`\`
Day ${day}${title != null ? " - " + title : ""}
Time part 1: ${
part1.time !== null && part1.solved
? toFixed(part1.time) + "ms"
: "-"
}
Time part 2: ${
part2.time !== null && part2.solved
? toFixed(part2.time) + "ms"
: "-"
}
Both parts: ${timeBoth !== 0 ? toFixed(timeBoth) + "ms" : "-"}
\`\`\`
`;
})
.join("\n\n");
const summary = stripIndents`
\`\`\`
Total stars: ${totalStars}/50
Total time: ${toFixed(totalTime)}ms
\`\`\`
`;
return [results, summary].join("\n\n");
};
const readmeYearMD = (language: string, config: YearConfig) => {
const lang = language === "ts" ? "TypeScript" : "JavaScript";
const dayBadges = renderYearDayBadges(config);
const results = renderYearResults(config);
return stripIndents`
<!-- Entries between SOLUTIONS and RESULTS tags are auto-generated -->
[](https://adventofcode.com/${config.year})
[](https://nodejs.org/en/download/)

[](https://github.com/terryaney/aoc-automation)
# 🎄 Advent of Code ${config.year} 🎄
## Solutions
<!--SOLUTIONS-->
${dayBadges}
<!--/SOLUTIONS-->
_Click a badge to go to the specific day._
---
## Results
<!--RESULTS-->
${results}
<!--/RESULTS-->
---
✨🎄🎁🎄🎅🎄🎁🎄✨
`;
};
export { renderYearDayBadges, renderYearResults };
export default readmeYearMD;