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
|
#!/usr/bin/env python3
import argparse
import colorama
import os
import random
import signal
import string
import subprocess
import sys
import tempfile
import threading
import time
import token
import tokenize
from colorama import Fore, Style
from difflib import SequenceMatcher
from pathlib import Path
location = Path(sys.argv[0]).parent
content = location / ".." / "content"
default_patterns = {
"cpp": ["*.cc", "*.C", "*.cpp", "*.cxx", "*.c++"],
"py": ["*.py", "*.py2", "*.py3", "*.cpy"],
"java": ["*.java"],
}
colorama.init()
clear = "\033[K"
def interrupt_handler(sig, frame):
print(f"\n{clear}{Fore.RED}aborted{Style.RESET_ALL}")
os._exit(1)
signal.signal(signal.SIGINT, interrupt_handler)
parser = argparse.ArgumentParser(description="TCR-Racer is a game to train your TCR typing ability.")
parser.add_argument("-p", dest="penalty", default="60", type=int, help='The penalty for "submitting" wrong code (in seconds)')
parser.add_argument("--patterns", default=None, nargs="+", help=f"Patterns to check which files in '{content}' should be considered")
for name, pattern in default_patterns.items():
pattern_text = ",".join(pattern)
parser.add_argument(f"--{name}", action="store_true", help=f"add {name} pattern ({pattern_text})")
parser.add_argument("--debug", action="store_true", help="Show debug information")
args = parser.parse_args()
selected_default_patterns = []
for name, pattern in default_patterns.items():
if getattr(args, name):
selected_default_patterns += pattern
if selected_default_patterns:
args.patterns = (args.patterns or []) + selected_default_patterns
def matches_pattern(f):
if args.patterns is None:
return True
return any(f.match(p) for p in args.patterns)
files = [f for f in content.rglob("*") if f.is_file() and f.suffix not in [".tex", ".sty"]]
filtered = [f for f in files if matches_pattern(f)]
print(f"Found {len(filtered)} matching files.")
if not filtered:
sys.exit(1)
selected = random.choice(filtered)
language = "unknown"
for name, pattern in default_patterns.items():
if any(selected.match(p) for p in pattern):
language = name
if language in ["cpp", "py", "java"]:
print(f"Language is: {language}, comments are ignored")
else:
print(f"{Fore.YELLOW}WARNING:{Style.RESET_ALL} Language is unknown, you must type comments")
def normalize(file):
if language in ["cpp", "java"]:
# call c preprocessor to remove macros, comments and stuff
with open(file) as f:
text = subprocess.check_output(["cpp", "-dD", "-P", "-fpreprocessed"], stdin=f, encoding="utf-8")
# remove whitespaces
for whitespace in string.whitespace:
text = text.replace(whitespace, "")
return text
elif language == "py":
previous = token.INDENT
cleaned = []
with open(file) as f:
for type, value, _, _, _ in tokenize.generate_tokens(f.readline):
# remove comment and multiline comment
if type == token.COMMENT:
continue
elif type == token.STRING and previous == token.INDENT:
continue
# remove whitespaces
cleaned.append(value.strip())
previous = type
return "".join(cleaned)
else:
text = file.read_text()
# remove whitespaces
for whitespace in string.whitespace:
text = text.replace(whitespace, "")
return text
expected = normalize(selected)
tmpfile = (Path(tempfile.gettempdir()) / "tcrracer").with_suffix(selected.suffix)
tmpfile.write_text("")
print("You have to type to: ", Fore.YELLOW, tmpfile, Style.RESET_ALL, sep="")
#print("Press Enter to start",end="")
input("Press Enter to start")
print()
print("You have to type: ", Fore.YELLOW, selected.relative_to(content), Style.RESET_ALL, sep="")
if args.debug:
print("Expected:", f"{Fore.YELLOW}{expected}{Style.RESET_ALL}")
print('Press enter to "submit" (or ctrl+c to abort)')
print()
accepted = False
tries = 0
start = time.perf_counter()
def edit_distance(a, b):
n, m = len(a), len(b)
dp = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(n + 1):
dp[i][0] = i
for j in range(m + 1):
dp[0][j] = j
for i in range(1, n + 1):
for j in range(1, m + 1):
if a[i - 1] == b[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1])
return dp[n][m]
def submit():
global accepted
global tries
while not accepted:
debug = input()
got = normalize(tmpfile)
equal = got == expected
verdict = f"{Fore.GREEN}AC{Style.RESET_ALL}" if equal else f"{Fore.RED}WA{Style.RESET_ALL}"
message = [f"{clear}submitted:", verdict]
if args.debug:
message.append(f"(distance: {edit_distance(got, expected)})")
if debug == "ac": equal = True
if debug == "wa": equal = False
print(*message)
if equal:
accepted = True
else:
tries += 1
threading.Thread(target=submit, daemon=True).start()
elapsed = 0
penalty = 0
while not accepted:
elapsed = time.perf_counter() - start
penalty = args.penalty * tries
print(f"time: {elapsed + penalty:.2f}s (try: {tries+1})", end="\r", flush=True)
time.sleep(0.075)
print()
print(f"time: {elapsed + penalty:.2f}s (tries: {tries+1})")
print(f"speed: ~{len(expected) / elapsed:.2f} chars per second", end="")
if penalty:
print(f", ~{len(expected) / (elapsed + penalty):.2f} with penalty", end="")
print()
|