import os, math, matplotlib.pyplot as plt

path = '/home/benjamin/Desktop/bens_folder/school/m2/internship/work/romaric/me/'

os.chdir(path)

def getDifficultyOfHash(hexHash):
    return int(hexHash, 16)

difficultyRatios = {}
X, Y = [], []

with open('hashes.txt') as f:
    lines = f.read().splitlines()
    for line in lines:
        lineParts = line.split()
        hash = lineParts[0]
        target = lineParts[1]
        hashDifficulty = getDifficultyOfHash(hash)
        targetDifficulty = getDifficultyOfHash(target)
        #difficultyRatio = targetDifficulty // hashDifficulty
        difficultyRatio = int(math.log2(targetDifficulty / hashDifficulty))
        if difficultyRatio in difficultyRatios:
            difficultyRatios[difficultyRatio] += 1
        else:
            difficultyRatios[difficultyRatio] = 1

for i in range(max(difficultyRatios) + 1):
    if i in difficultyRatios:
        X += [i]
        Y += [difficultyRatios[i] / len(lines)]

## To improve the readability:

#plt.semilogx(X, Y, base = 2)
XMain = [2 ** i for i in range(math.ceil(math.log2(X[-1])))]
XP = [0] + XMain + ([X[-1]] if not X[-1] in XMain else [])
YP = [difficultyRatios[difficultyRatio] / len(lines) for difficultyRatio in XP]

plt.scatter(XP, YP)

plt.xticks(XP, XP)

upperBound = math.ceil(math.log2(X[-1])) + 2
YP = [2 ** -i for i in range(upperBound)]

plt.yticks(YP, YP)

for x, y in zip(X, Y):
    if x == 0 or math.log2(x) == int(math.log2(x)):
        plt.text(x, y, f'({x}, {round(y, 4)})', ha='center', va='top')

##

plt.plot(X, Y)

plt.show()
