LCOV - code coverage report
Current view: top level - 15 - a.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 34 34
Test Date: 2025-10-12 18:20:59 Functions: 100.0 % 2 2
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 95.0 % 40 38

             Branch data     Line data    Source code
       1                 :             : #include <iostream>
       2                 :             : #include <fstream>
       3                 :             : #include <vector>
       4                 :             : #include <unordered_map>
       5                 :             : #include <string>
       6                 :             : 
       7                 :             : using namespace std;
       8                 :             : 
       9                 :             : static const unordered_map<char, pair<int, int>> deltas = {{'<', {-1, 0}}, {'>', {1, 0}}, {'^', {0, -1}}, {'v', {0, 1}}};
      10                 :        6026 : bool moveBox(vector<string>& grid, int& gpsSum, const pair<int, int>& box, const pair<int, int>& delta) {
      11                 :        6026 :   if (auto newBox = pair<int, int>{box.first + delta.first, box.second + delta.second};
      12   [ +  +  +  + ]:        9515 :       grid[newBox.second][newBox.first] == '.' ||
      13   [ +  +  +  + ]:        3489 :           (grid[newBox.second][newBox.first] == 'O' && moveBox(grid, gpsSum, newBox, delta))) {
      14                 :        4334 :     gpsSum += 100 * delta.second + delta.first;
      15                 :        4334 :     grid[newBox.second][newBox.first] = 'O';
      16                 :        4334 :     return true;
      17                 :             :   }
      18                 :        1692 :   return false;
      19                 :             : }
      20                 :             : 
      21                 :           2 : int main() {
      22                 :           2 :   ifstream input("input.txt");
      23         [ +  + ]:           2 :   if (!input) {
      24                 :           1 :     cerr << "Error: input.txt not found" << endl;
      25                 :           1 :     return 1;
      26                 :             :   }
      27                 :           1 :   vector<string> grid;
      28                 :           1 :   pair<int, int> robot;
      29                 :           1 :   int gpsSum = 0;
      30   [ +  -  +  +  :          51 :   for (string line; getline(input, line) && !line.empty(); grid.push_back(std::move(line))) {
                   +  + ]
      31         [ +  + ]:        2550 :     for (int x = 0; x < line.size(); ++x) {
      32         [ +  + ]:        2500 :       if (line[x] == '@') {
      33                 :           1 :         robot = {x, static_cast<int>(grid.size())};
      34         [ +  + ]:        2499 :       } else if (line[x] == 'O') {
      35                 :         589 :         gpsSum += 100 * (grid.size() ) + x;
      36                 :             :       }
      37                 :             :     }
      38                 :           1 :   }
      39                 :             : 
      40   [ +  +  +  -  :          21 :   for (string line; getline(input, line) && !line.empty();) {
                   +  + ]
      41         [ +  + ]:       20020 :     for (char move : line) {
      42                 :       20000 :       auto [dx, dy] = deltas.at(move);
      43                 :       20000 :       pair<int, int> newRobot = {robot.first + dx, robot.second + dy};
      44   [ +  +  +  +  :       20000 :       if ((grid[newRobot.second][newRobot.first] == 'O' && moveBox(grid, gpsSum, newRobot, {dx, dy})) || grid[newRobot.second][newRobot.first] == '.') {
             +  +  +  + ]
      45                 :       17623 :         grid[newRobot.second][newRobot.first] = '@';
      46                 :       17623 :         grid[robot.second][robot.first] = '.';
      47                 :       17623 :         robot = newRobot;
      48                 :             :       }
      49                 :             :     }
      50                 :           1 :   }
      51                 :             : 
      52                 :           1 :   cout << gpsSum << endl;
      53         [ +  + ]:           3 : }
        

Generated by: LCOV version 2.0-1