LCOV - code coverage report
Current view: top level - 05 - b.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 27 27
Test Date: 2025-10-12 18:20:59 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 95.5 % 22 21

             Branch data     Line data    Source code
       1                 :             : #include <iostream>
       2                 :             : #include <fstream>
       3                 :             : #include <sstream>
       4                 :             : #include <vector>
       5                 :             : #include <unordered_map>
       6                 :             : #include <unordered_set>
       7                 :             : #include <algorithm>
       8                 :             : using namespace std;
       9                 :             : 
      10                 :           2 : int main() {
      11                 :           2 :   ifstream inputFile("input.txt");
      12         [ +  + ]:           2 :   if (!inputFile) {
      13                 :           1 :     cerr << "Error opening file." << endl;
      14                 :           1 :     return 1;
      15                 :             :   }
      16                 :             : 
      17                 :           1 :   unordered_map<int, unordered_set<int>> rules;
      18                 :             : 
      19   [ +  -  +  +  :        1177 :   for (string line; getline(inputFile, line) && line.contains('|');) {
                   +  + ]
      20                 :             :     int from;
      21                 :             :     int to;
      22                 :             :     char delim; 
      23                 :        1176 :     stringstream(line) >> from >> delim >> to;
      24                 :        1176 :     rules[from].insert(to);
      25                 :           1 :   }
      26                 :             : 
      27                 :           1 :   int middleSum = 0;
      28         [ +  + ]:         201 :   for (string line; getline(inputFile, line);) {
      29                 :         200 :     stringstream ss(line);
      30                 :         200 :     vector<int> seq;
      31                 :         200 :     unordered_set<int> pos;
      32                 :         200 :     bool isInvalid = false;
      33                 :             : 
      34         [ +  + ]:        3110 :     for (int n; ss >> n; ss.ignore(1, ',')) {
      35   [ +  +  +  +  :       41632 :       if (!isInvalid && !ranges::all_of(rules[n], [&pos](int to) { return !pos.contains(to); })) {
                   +  + ]
      36                 :         110 :         isInvalid = true;
      37                 :             :       }
      38                 :        2910 :       seq.push_back(n);
      39                 :        2910 :       pos.insert(n);
      40                 :             :     }
      41         [ +  + ]:         200 :     if(isInvalid) {
      42                 :        5112 :       ranges::stable_sort(seq, [&rules](int a, int b) { return rules[a].contains(b); });
      43                 :         110 :       middleSum += seq[seq.size() / 2];
      44                 :             :     }
      45                 :         201 :   }
      46                 :             : 
      47                 :           1 :   cout << middleSum << endl;
      48         [ +  + ]:           3 : }
        

Generated by: LCOV version 2.0-1