| 1 |
|
| 2 |
|
| 3 |
from __future__ import print_function |
| 4 |
import random |
| 5 |
|
| 6 |
DIFFICULTY_TARGET = 120 |
| 7 |
DIFFICULTY_WINDOW = 720 |
| 8 |
DIFFICULTY_LAG = 15 |
| 9 |
DIFFICULTY_CUT = 60 |
| 10 |
|
| 11 |
def difficulty(): |
| 12 |
times = [] |
| 13 |
diffs = [] |
| 14 |
while True: |
| 15 |
if len(times) <= 1: |
| 16 |
diff = 1 |
| 17 |
else: |
| 18 |
begin = max(len(times) - DIFFICULTY_WINDOW - DIFFICULTY_LAG, 0) |
| 19 |
end = min(begin + DIFFICULTY_WINDOW, len(times)) |
| 20 |
length = end - begin |
| 21 |
assert length >= 2 |
| 22 |
if length <= DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT: |
| 23 |
cut_begin = 0 |
| 24 |
cut_end = length |
| 25 |
else: |
| 26 |
excess = length - (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) |
| 27 |
cut_begin = (excess + 1) // 2 |
| 28 |
cut_end = length - excess // 2 |
| 29 |
assert cut_begin + 2 <= cut_end |
| 30 |
wnd = times[begin:end] |
| 31 |
wnd.sort() |
| 32 |
dtime = wnd[cut_end - 1] - wnd[cut_begin] |
| 33 |
dtime = max(dtime, 1) |
| 34 |
ddiff = sum(diffs[begin + cut_begin + 1:begin + cut_end]) |
| 35 |
diff = (ddiff * DIFFICULTY_TARGET + dtime - 1) // dtime |
| 36 |
times.append((yield diff)) |
| 37 |
diffs.append(diff) |
| 38 |
|
| 39 |
random.seed(1) |
| 40 |
time = 1000 |
| 41 |
gen = difficulty() |
| 42 |
diff = next(gen) |
| 43 |
for i in range(100000): |
| 44 |
power = 100 if i < 10000 else 100000000 if i < 500 else 1000000000000 if i < 1000 else 1000000000000000 if i < 2000 else 10000000000000000000 if i < 4000 else 1000000000000000000000000 |
| 45 |
time += random.randint(-diff // power - 10, 3 * diff // power + 10) |
| 46 |
print(time, diff) |
| 47 |
diff = gen.send(time) |