<?php

$inputFile = __DIR__ . '/count.txt';
$outputFile = __DIR__ . '/count_divided.txt';

// Pročitaj sadržaj fajla
$lines = file($inputFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Očisti output fajl
file_put_contents($outputFile, '');

$total = 0;
$results = [];

foreach ($lines as $line) {
    // Razdvoji liniju na delove
    $parts = explode(': ', $line);
    if (count($parts) === 2) {
        $yearMonth = explode(' -> ', $parts[0]);
        $year = intval($yearMonth[0]);
        $month = $yearMonth[1];
        $number = intval($parts[1]);
        
        // Podeli broj sa 2
        $divided = $number / 2;
        
        // Dodaj u total
        $total += $divided;
        
        // Grupiši po godinama
        if (!isset($results[$year])) {
            $results[$year] = [];
        }
        $results[$year][$month] = $divided;
    }
}

// Sortiraj po godinama
ksort($results);

// Upisi sortirane rezultate
foreach ($results as $year => $months) {
    // Sortiraj mesece
    ksort($months);
    foreach ($months as $month => $divided) {
        $result = $year . ' -> ' . $month . ': ' . $divided . "\n";
        file_put_contents($outputFile, $result, FILE_APPEND);
    }
}

// Dodaj total na kraju
$totalLine = "\nTOTAL: " . $total . "\n";
file_put_contents($outputFile, $totalLine, FILE_APPEND);
?> 