forked from Yawn-Sean/Daily_CF_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
records.py
114 lines (101 loc) · 3.59 KB
/
records.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
import sys
import os
from datetime import date, datetime, timedelta
input_file = sys.argv[1]
output_file = sys.argv[2]
project_root = sys.argv[3]
dc = {}
if os.path.exists(input_file):
print('Found existing input file ' + input_file)
with open(input_file, 'r') as f:
for line in f.readlines():
line = line.strip()
if ':' not in line:
continue
k, v = line.split(':')
dc[k] = v
else:
print('Cannot find existing input file ' + input_file)
current_date = date(2024, 2, 25)
if 'current_date' in dc.keys():
dt = datetime.strptime(dc['current_date'], '%Y-%m-%d')
current_date = date(dt.year, dt.month, dt.day)
if current_date < date(2024, 3, 8):
print('Full re-calculate by intention')
current_date = date(2024, 2, 25)
dc = {}
latest_date = current_date
users = {}
for k, v in dc.items():
if k == 'current_date' or k == 'skipped_dates':
continue
users[k] = list(map(lambda s: list(map(int, s.split(','))), v.split('/')))
skipped_dates = []
if 'skipped_dates' in dc.keys():
y, m, d = 0, 0, 0
for s in dc['skipped_dates'].split(','):
if s[0] == 'y':
y = int(s[1:])
elif s[0] == 'm':
m = int(s[1:])
else:
for i in range(0, len(s), 2):
d = int(s[i:i + 2])
skipped_dates.append('%d%02d%02d' % (y, m, d))
yesterday = date.today() - timedelta(days = 1)
while current_date < yesterday:
current_date += timedelta(days = 1)
dirname = '%s/daily_problems/%d/%02d/%02d%02d/personal_submission' % (project_root, current_date.year, current_date.month, current_date.month, current_date.day)
if not os.path.exists(dirname):
skipped_dates.append('%d%02d%02d' % (current_date.year, current_date.month, current_date.day))
continue
submissions = {}
for filename in os.listdir(dirname):
if not '_' in filename:
continue
lst = filename.split('.')[0].split('_')
pid, uid = lst[0].lower(), '_'.join(lst[1:]).lower()
if uid.endswith('_star'):
uid = uid[:-5]
if uid not in submissions.keys():
submissions[uid] = set()
submissions[uid].add(pid)
for uid, lst in users.items():
if uid in submissions.keys():
sz = min(2, len(submissions[uid]))
else:
sz = 0
if lst[-1][0] == sz:
lst[-1][1] += 1
else:
lst.append([sz, 1])
for uid, st in submissions.items():
sz = min(2, len(st))
if uid not in users.keys():
users[uid] = [[sz, 1]]
latest_date = current_date
with open(output_file, 'w') as f:
f.write('let records = `\n')
f.write('current_date:%s\n' % latest_date.strftime('%Y-%m-%d'))
for uid, lst in users.items():
s = '/'.join(map(lambda p: str(p[0]) + ',' + str(p[1]), lst))
f.write('%s:%s\n' % (uid, s))
f.write('skipped_dates:')
y, m = 0, 0
lst = []
for s in skipped_dates:
if s > '%d%02d%02d' % (latest_date.year, latest_date.month, latest_date.day):
break
yy, mm, dd = int(s[:4]), int(s[4:6]), int(s[6:])
if y != yy:
lst.append('y%d' % yy)
y = yy
if m != mm:
lst.append('m%d' % mm)
m = mm
if lst[-1][0] == 'y' or lst[-1][0] == 'm':
lst.append('')
lst[-1] += '%02d' % dd
f.write(','.join(lst))
f.write('\n')
f.write('`;\n')