forked from vEnhance/von
-
Notifications
You must be signed in to change notification settings - Fork 0
/
puid.py
245 lines (237 loc) · 6.29 KB
/
puid.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import hashlib
import re
lookup = {
"Afghanistan": "AFG",
"Albania": "ALB",
"Algeria": "ALG",
"Angola": "AGO",
"Argentina": "ARG",
"Armenia": "ARM",
"Australia": "AUS",
"Austria": "AUT",
"Azerbaijan": "AZE",
"Bahrain": "BAH",
"Bangladesh": "BGD",
"Belarus": "BLR",
"Belgium": "BEL",
"Benin": "BEN",
"Bolivia": "BOL",
"Bosnia and Herzegovina": "BIH",
"Botswana": "BWA",
"Brazil": "BRA",
"Brunei": "BRU",
"Bulgaria": "BGR",
"Burkina Faso": "BFA",
"Cambodia": "KHM",
"Canada": "CAN",
"Chile": "CHI",
"China": "CHN",
"Colombia": "COL",
"Commonwealth of Independent States": "CIS",
"Costa Rica": "CRI",
"Croatia": "HRV",
"Cuba": "CUB",
"Cyprus": "CYP",
"Czech": "CZE",
"Czechoslovakia": "CZS",
"Denmark": "DEN",
"Dominican Republic": "DOM",
"Ecuador": "ECU",
"Egypt": "EGY",
"Estonia": "EST",
"Finland": "FIN",
"France": "FRA",
"Gambia": "GMB",
"Georgia": "GEO",
"German Democratic Republic": "GDR",
"Germany": "GER",
"Ghana": "GHA",
"Greece": "HEL",
"Guatemala": "GTM",
"Honduras": "HND",
"Hong Kong": "HKG",
"Hungary": "HUN",
"Iceland": "ISL",
"India": "IND",
"Indonesia": "IDN",
"Iraq": "IRQ",
"Iran": "IRN",
"Ireland": "IRL",
"Israel": "ISR",
"Italy": "ITA",
"Ivory Coast": "CIV",
"Jamaica": "JAM",
"Japan": "JPN",
"Kazakhstan": "KAZ",
"Kenya": "KEN",
"North Korea": "PRK",
"Korea": "KOR",
"Kosovo": "KSV",
"Kuwait": "KWT",
"Kyrgyzstan": "KGZ",
"Laos": "LAO",
"Latvia": "LVA",
"Liechtenstein": "LIE",
"Lithuania": "LTU",
"Luxembourg": "LUX",
"Macau": "MAC",
"Macedonia": "MKD",
"Madagascar": "MDG",
"Malaysia": "MAS",
"Mauritania": "MRT",
"Mexico": "MEX",
"Moldova": "MDA",
"Mongolia": "MNG",
"Montenegro": "MNE",
"Morocco": "MAR",
"Mozambique": "MOZ",
"Myanmar": "MMR",
"Nepal": "NPL",
"Netherlands": "NLD",
"New Zealand": "NZL",
"Nicaragua": "NIC",
"Nigeria": "NGA",
"North Macedonia": "MKD",
"Norway": "NOR",
"Oman": "OMN",
"Pakistan": "PAK",
"Panama": "PAN",
"Paraguay": "PAR",
"Peru": "PER",
"Philippines": "PHI",
"Poland": "POL",
"Portugal": "POR",
"Puerto Rico": "PRI",
"Romania": "ROU",
"Russia": "RUS",
"El Salvador": "SLV",
"Saudi Arabia": "SAU",
"Senegal": "SEN",
"Serbia": "SRB",
"Serbia and Montenegro": "SCG",
"Singapore": "SGP",
"Slovakia": "SVK",
"Slovenia": "SVN",
"South Africa": "SAF",
"Spain": "ESP",
"Sri Lanka": "LKA",
"Sweden": "SWE",
"Switzerland": "SUI",
"Syria": "SYR",
"Taiwan": "TWN",
"Tajikistan": "TJK",
"Tanzania": "TZA",
"Thailand": "THA",
"Trinidad and Tobago": "TTO",
"Tunisia": "TUN",
"Turkey": "TUR",
"Turkmenistan": "TKM",
"Uganda": "UGA",
"Ukraine": "UKR",
"United Arab Emirates": "UAE",
"United Kingdom": "UNK",
"Uruguay": "URY",
"Uzbekistan": "UZB",
"Venezuela": "VEN",
"Vietnam": "VNM",
"Yemen": "YEM",
"Yugoslavia": "YUG",
"Zimbabwe": "ZWE",
}
for k, v in list(lookup.items()):
lookup[k + " MO"] = v + "MO"
lookup[k + " TST"] = v + "TST"
lookup[k + " GST"] = v + "EST" # GMO selection test
lookup[k + " RMM TST"] = v + "RST"
lookup[k + " EGMO TST"] = v + "EST"
lookup[k + " JBMO TST"] = v + "JST"
lookup[k + " TSTST"] = v + "TSTST"
lookup["AMC 10A"] = "10A"
lookup["AMC 10B"] = "10B"
lookup["AMC 12A"] = "12A"
lookup["AMC 12B"] = "12A"
lookup["ARML Local"] = "ARMLOC"
lookup["Balkan"] = "BALK"
lookup["Baltic Way"] = "BWAY"
lookup["Brazil Revenge"] = "BRAR"
lookup["Brazil Undergrad"] = "BRAU"
lookup["Catalunya"] = "CTLNYA"
lookup["Centroamerican"] = "CENTRO"
lookup["China TST Quiz"] = "CHNQ"
lookup["CodeForces"] = "CF"
lookup["Cono Sur"] = "CSUR"
lookup["Cyberspace Competition"] = "CYBER"
lookup["Czech Polish Slovak"] = "CPS"
lookup["December TST"] = "DECTST"
lookup["ELMO Revenge"] = "ELMOR"
lookup["ELMO SL"] = "ESL"
lookup["European Cup"] = "EURCUP"
lookup["HMMT"] = "HMMT"
lookup["Feb HMMT IntBee"] = "FEBIB"
lookup["Nov HMMT IntBee"] = "NOVIB"
lookup["CMM IntBee"] = "CMMIB"
lookup["IntBee Quals"] = "MIBQT"
lookup["IntBee"] = "MITIB"
lookup["HMNT"] = "HMNT"
lookup["Iberoamerican"] = "IBERO"
lookup["InftyDots"] = "IDOTS"
lookup["January TST"] = "JANTST"
lookup["KoMaL"] = "KML"
lookup["Kurschak"] = "KSK"
lookup["Longlist"] = "LL"
lookup["math.SE"] = "MSE"
lookup["MP4G"] = "MP4G"
lookup["Math Prize"] = "MPO"
lookup["Napkin"] = "NAP"
lookup["NIMO Winter"] = "NIMOW"
lookup["PUMaC Finals"] = "PUF"
lookup["Putnam"] = "PTNM"
lookup["Schweitzer"] = "MSZ"
lookup["Serbia RMM TST"] = "SRBRST"
lookup["Sharygin"] = "SHRG"
lookup["Shortlist"] = "SL"
lookup["St Petersburg"] = "SPBRG"
lookup["Taiwan Quiz"] = "TWNQ"
lookup["ToT Fall"] = "TTF"
lookup["ToT Spring"] = "TTS"
lookup["Tuymaada"] = "TMD"
lookup["Twitch"] = "TWCH"
lookup["USAMO"] = "AMO"
lookup["USAMTS"] = "USMT"
lookup["USA GST"] = "USAEST"
# whooooo
REGEX = r"(?P<contest>[a-zA-Z][a-zA-Z0-9 ]+)(19|20)(?P<year>[0-9][0-9])(?P<stem>[ \/](?P<locator>[0-9A-Za-z\.\/\- ]+))?$"
re_generic = re.compile(REGEX)
sorted_lookup_keys = list(lookup.keys())
sorted_lookup_keys.sort(key=lambda x: (-len(x), x))
def getOnlyAlphanum(s: str) -> str:
return re.sub(r"[^A-Z0-9]", "", s.upper())
def inferPUID(source: str) -> str:
source = source.replace("Finals", "F")
if (m := re_generic.match(source)) is not None:
d = m.groupdict()
contest = d["contest"].strip()
source = (
d["year"]
+ lookup.get(contest, getOnlyAlphanum(contest))
+ getOnlyAlphanum(d["stem"] or "")
)
if contest in lookup:
return source
thresh = 11
elif source[0] == "H" and source[1:].isdigit():
return source
else:
for k in sorted_lookup_keys:
if source.startswith(k):
source = lookup[k] + source[len(k) :]
thresh = 11
break
else:
thresh = 8
source = getOnlyAlphanum(source)
if len(source) <= thresh:
return source
else:
# still too long, return some sort of hash
return "Z" + (hashlib.sha256(source.encode("ascii")).hexdigest())[0:7].upper()