import os
def count_max_numbers(source_file, result_file):
    # Проверяем и создаем директорию для результатов
    result_dir = os.path.dirname(result_file)
    if not os.path.exists(result_dir):
        os.makedirs(result_dir)
        print(f"Created directory {result_dir}")

    # Чтение чисел из source_file
    try:
        with open(source_file, 'r') as f:
            numbers = [int(line.strip()) for line in f if line.strip().isdigit()]
    except FileNotFoundError:
        print(f"File not found: {source_file}")
        return

    if not numbers:
        print(f"No valid numbers found in {source_file}")
        return

    # Находим максимум
    max_number = max(numbers)
    max_count = numbers.count(max_number)

    # Записываем количество максимальных чисел в result_file
    with open(result_file, 'w') as f_out:
        f_out.write(f"{max_count}")

    print(f"Wrote count of max number {max_number} to {result_file}")


if __name__ == "__main__":

    count_max_numbers('/var/result/data.txt', '/var/result/result.txt')
