forked from lxylxy123456/AdventOfCode2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_readme.py
More file actions
118 lines (104 loc) · 2.74 KB
/
gen_readme.py
File metadata and controls
118 lines (104 loc) · 2.74 KB
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
import os, re, jinja2
t = jinja2.Template('''
# Advent of Code 2024
<https://adventofcode.com/2024>
## Files
| day | question | example | input | solution | Youtube | other |
|-----|----------|---------|-------|----------|---------|----------|
{%- for day in days %}
|{{ day.dir }}|{{ day.qs }}|{{ day.exs }}|{{ day.ins }}|{{ day.sols }}|{{ day.youtube }}|{{ day.others }}|
{%- endfor %}
## My time
```
{{ my_time }}
```
'''.lstrip('\n'))
dict_render = {
'days': []
}
def file_link(href, name=None):
if name is None:
name = os.path.basename(href)
return '[%s](%s)' % (name, href)
def get_youtube_link(d):
line = open(os.path.join(d, 's.py')).read().split('\n')[0]
matched = re.fullmatch(r'# Youtube: https://youtu\.be/(.{11})', line)
if not matched:
return ''
link, = matched.groups()
readme_content = ''.join([
'Youtube Video:\n',
'\n',
'[]' % link,
'(http://www.youtube.com/watch?v=%s)\n' % link,
])
readme_path = os.path.join(d, 'README.md')
if os.path.exists(readme_path):
assert open(readme_path).read() == readme_content
else:
open(readme_path, 'w').write(readme_content)
return file_link('https://youtu.be/%s' % link, '`%s`' % link)
for i in range(1, 26):
d = 'd%02d' % i
if not os.path.exists(d):
print('Does not exist:', repr(d))
continue
files = sorted(os.listdir(d))
qs = []
exs = []
ins = []
sols = []
others = []
youtube = get_youtube_link(d)
for i in list(files):
matched = re.fullmatch(r'ex(.*)\.txt', i)
if matched:
if i == 'ex.txt':
exs.append(file_link(d + '/' + i))
else:
exs.append(file_link(d + '/' + i, matched.groups()[0]))
files.remove(i)
continue
if i == 'in.txt':
ins.append(file_link(d + '/' + i))
files.remove(i)
continue
if i == 'q.txt':
qs.append(file_link(d + '/' + i))
files.remove(i)
continue
matched = re.fullmatch(r's(.*)\.py', i)
if matched:
if i == 's.py':
sols.append(file_link(d + '/' + i))
else:
sols.append(file_link(d + '/' + i, matched.groups()[0]))
files.remove(i)
continue
if i == 'README.md':
#youtube = get_youtube_link(d)
files.remove(i)
continue
if i in ['compute.sh']:
files.remove(i)
continue
if 1:
others.append(file_link(d + '/' + i))
day = {
'name': d,
'dir': file_link(d),
'qs': ', '.join(qs),
'exs': ', '.join(exs),
'ins': ', '.join(ins),
'sols': ', '.join(sols),
'others': ', '.join(others),
'youtube': youtube,
}
dict_render['days'].append(day)
lines = open('time.txt').read().split('\n')[1:]
while lines and '-Part 1-' not in lines[0] and '-Part 2-' not in lines[0]:
lines.pop(0)
while lines and lines[-1] == '':
lines.pop()
dict_render['my_time'] = '\n'.join(lines)
print(t.render(**dict_render), file=open('README.md', 'w'))