|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +from pathlib import Path |
| 5 | +from datetime import datetime |
| 6 | +from collections import Counter |
| 7 | + |
| 8 | +from matplotlib import pyplot as plt |
| 9 | + |
| 10 | +directory = Path(__file__).parent |
| 11 | + |
| 12 | +def get_languages_used(path: Path): |
| 13 | + """ Returns (count, language) tuples, e.g.: [(1, '.apl'), (3, '.py'), (24, '.jl')] """ |
| 14 | + ignore = [".in", ".run", ".sh", ".ans", "", ".cookie"] |
| 15 | + occ = Counter(file.suffix for file in path.glob("**/*") if file.suffix not in ignore) |
| 16 | + return sorted(zip(occ.values(), occ.keys())) |
| 17 | + |
| 18 | +def make_plot_for_year(year: int, lang="auto"): |
| 19 | + year_dir = directory / str(year) |
| 20 | + if lang == "auto": |
| 21 | + lang = get_languages_used(year_dir)[-1][1] |
| 22 | + if lang[0] != '.': |
| 23 | + lang = f".{lang}" |
| 24 | + |
| 25 | + loc = [] |
| 26 | + for day_dir in filter(lambda p: p.is_dir(), year_dir.iterdir()): |
| 27 | + names = [f"{day_dir.name}", f"{int(day_dir.name)}"] |
| 28 | + for name in names: |
| 29 | + if (filepath := day_dir / f"{name}{lang}").exists(): |
| 30 | + with open(filepath, "r") as file: |
| 31 | + lines = [*filter(lambda line: line.strip() != "", file.read().strip().split("\n"))] |
| 32 | + loc.append(len(lines)) |
| 33 | + break |
| 34 | + xticks = [*range(1, len(loc)+1)] |
| 35 | + bars = plt.bar(xticks, loc) |
| 36 | + for bar, line_count in zip(bars, loc): |
| 37 | + plt.text(bar.get_x() + .45, line_count + 1, line_count, ha="center") |
| 38 | + plt.title(f"Lines of code for each '{lang}' solution in Advent of Code {year}") |
| 39 | + plt.xticks(xticks) |
| 40 | + plt.yticks(range(0, 130, 10)) |
| 41 | + plt.ylabel("Lines of code") |
| 42 | + plt.xlabel("Day") |
| 43 | + # plt.show() |
| 44 | + plt.savefig(directory / "Media" / f"{year}loc.png") |
| 45 | + |
| 46 | + |
| 47 | +def main(): |
| 48 | + date = datetime.now() |
| 49 | + year = date.year - (date.month <= 11) |
| 50 | + parser = argparse.ArgumentParser("Make fancy plot (not really)") |
| 51 | + parser.add_argument("-y", "--year", default=year) |
| 52 | + parser.add_argument("-l", "--lang", default="auto") |
| 53 | + parsed = parser.parse_args() |
| 54 | + |
| 55 | + make_plot_for_year(parsed.year, parsed.lang) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + main() |
0 commit comments