Skip to content

Commit fdd3cd9

Browse files
committed
Add lines of code plot
1 parent add6882 commit fdd3cd9

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

Media/2021loc.png

28.6 KB
Loading

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ All solutions expect the input via stdin, which can be easily achieved by piping
55
Programs are initialized with the `init-day.sh` script. I.e. typing `init-day.sh 10` initializes the 10th day by creating a folder named `10`, downloading the input test case with the `session.cookie`, copying the `dummy.py` file and opening the solution file.
66

77
* 2021: There is a solution for each problem in Julia. Sometimes there is a Python or APL solution as well. I'm trying out Julia for the first time, mostly focusing on short and elegant code.
8+
9+
## Lines of code
10+
11+
![](./Media/2021loc.png)

loc-plot.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)