This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
60 lines (48 loc) · 1.45 KB
/
main.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
from itertools import count
from typing import Literal
POLY: Literal[0xEDB88320] = 0xEDB88320
table: list[int] = []
last_index: list[int] = [0] * 256
for i in range(256):
value = i
for _ in range(8):
if value & 1:
value = value >> 1 ^ POLY
else:
value >>= 1
table.append(value)
last_index[value >> 24] = i
def crc32(string: str) -> tuple[int, int]:
crc: int = 0xFFFFFFFF
index: int = 0xFF
for i in string:
index = (crc ^ ord(i)) & 0xFF
crc = crc >> 8 ^ table[index]
return crc, index
def check(high: int, indexes: list) -> int | None:
crc, index = crc32(str(high))
if index != indexes[3]:
return
high *= 1000
for i in range(2, -1, -1):
num = (crc & 0xFF ^ indexes[i]) - 48
if not 0 <= num < 10:
return
high += num * 10 ** i
crc = table[indexes[i]] ^ crc >> 8
return high
def main(crc: str | int) -> int: # type: ignore
indexes = [0] * 4
crc: int = (crc if isinstance(crc, int) else int(crc, 16)) ^ 0xFFFFFFFF
for i in range(1, 1000):
if crc == crc32(str(i))[0]:
return i
for i in range(3, -1, -1):
index = indexes[3 - i] = last_index[crc >> (i << 3)]
crc ^= table[index] >> ((3 - i) << 3)
for i in count(1):
if result := check(i, indexes):
return result
if __name__ == "__main__":
from sys import argv
print(main(argv[1]))