-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_completion.py
122 lines (97 loc) · 3.6 KB
/
generate_completion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import importlib
import inspect
import os
import re
from typing import Callable
import requests
from bs4 import BeautifulSoup
# This file simply generates a file called "completed.md" which contains a table of all the solutions that have been completed
COMPLETED = '✔' # Alternative: ✓
NOT_COMPLETED = '❌'
SEMI_COMPLETED = '½'
NOT_ATTEMPTED = '🚫'
def generate_table(solutions):
# This will manage the top of the table
sample_solution = [*solutions.values()]
days = len(sample_solution[0])
table = '||' + '|'.join(str(i) for i in range(1, days+1)) + '|\n'
# Seperator
table += '|' + '|'.join(['---'] * (days+1)) + '|\n'
for year, solution in solutions.items():
table += f'|**{year}**|{"|".join(solution)}|\n'
return table
def isEmpty(func:Callable):
'''Checks if the function does nothing'''
source = inspect.getsource(func).split('\n')
return source[1].strip() in ('pass','...') and len(source) == 3
def check_day(year:int, day:int):
try:
module = importlib.import_module(f'{year}.{day}')
except ModuleNotFoundError:
return NOT_ATTEMPTED
if str(module.completed) == str(1):
# Only runned if one parts is incomplete
return SEMI_COMPLETED
elif module.completed == True:
return COMPLETED
else:
return NOT_COMPLETED
def get_all_years():
resp = requests.get('https://adventofcode.com/events')
soup = BeautifulSoup(resp.text, 'html.parser')
# Get all the years
years = [elem.text for elem in soup.find_all('div', class_="eventlist-event")]
# Getting the year out from the raw text
match = re.compile(r'\[(\d+)\]')
years = [match.search(year).groups()[0] for year in years]
return years
def get_year_completion(year:str):
# Check all the days from 1 to 25
solutions = [check_day(year, day) for day in range(1, 26)]
print(f"Generated solution for year {year}: {' '.join(solutions)}")
return solutions
def get_code_count() -> int:
'''Returns the number of lines of code in the project'''
count = 0
for dir in os.listdir('.'):
if not os.path.isdir(dir): continue
for file in os.listdir(dir):
if not file.endswith('.py'): continue
with open(f'{dir}/{file}', 'r', encoding='utf-8') as f:
lines = [1 for line in f.readlines() if line.strip() != '' or line.strip().startswith('#')]
count += sum(lines)
return count
def generate_completion():
years = get_all_years()
print("Number Of years we are looking: ", len(years))
solutions = {year: get_year_completion(year) for year in years}
print("Generated solution status!")
table = generate_table(solutions)
print("Generated Solution table!")
flattened_solutions = []
for year in solutions.values():
flattened_solutions += year
parts_completed = flattened_solutions.count(COMPLETED) * 2 + flattened_solutions.count(SEMI_COMPLETED)
days_completed = flattened_solutions.count(COMPLETED)
# String That Will be written to the file
file_text = f'''
## Completion Status
Over Here you can see the completion status of the project.
Parts Completed: {parts_completed}/{len(flattened_solutions) * 2} ({parts_completed / (len(flattened_solutions) * 2) * 100:,.2f}%)
<br>
Days Completed: {days_completed}/{len(flattened_solutions)} ({days_completed / (len(flattened_solutions)) * 100:,.2f}%)
<br>
Lines of code: {get_code_count()}
### Legend
- {COMPLETED} = Part Completed
- {SEMI_COMPLETED} = One of the parts have been completed
- {NOT_COMPLETED} = The Day has been attempted but not completed
- {NOT_ATTEMPTED} = Day has not even been attempted
{table}
'''.strip()
print("Generated File Text")
with open('Completed.md', 'w', encoding='utf-8') as f:
f.write(file_text)
print("Wrote to file!")
if __name__ == '__main__':
generate_completion()