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)
        # If use `round` instead of `int`, then it gives the same result as before.
        difficultyRatio = round(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.scatter(X, Y)

plt.xticks(X, X)

YP = [2 ** -i for i in range(len(X))][:7]

plt.yticks(YP, YP)

for x, y in zip(X, Y):
    if x <= 5:
        plt.text(x, y, f'({x}, {round(y, 4)})', ha='center', va='top')

##

plt.plot(X, Y)

plt.show()

## To understand the difference between `int` and `round:`

import numpy as np

x = np.linspace(0, 10, 1000)

plt.plot(x, np.vectorize(int)(x), label = 'int')
plt.plot(x, np.vectorize(round)(x), label = 'round')

plt.legend(loc = 'upper left')

plt.show()

