Nicba1010

Day5v2

Dec 5th, 2020
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. /**
  2.  * Copyright (c) 2020 Nicba1010 All rights reserved.
  3.  */
  4.  
  5. #include <fstream>
  6. #include <iostream>
  7. #include <vector>
  8. #include <ctgmath>
  9.  
  10. /**
  11.  * @brief Finds the desired value by searching through the lower and upper bound by splitting the
  12.  *        search area in half every instruction.
  13.  *
  14.  * @details Finds the desired value by searching through the lower and upper bound by splitting the
  15.  *          search area in half every instruction according to the following rules.<br>
  16.  *          If the lowerBoundDesignator is detected it keeps the lower bound, but subtracts from
  17.  *          the upper bound using the following equation.
  18.  *          @code upperBound -= (upperBound - lowerBound + 1) / 2 @endcode
  19.  *          If the upperBoundDesignator is detected it keeps the upper bound, but adds to the
  20.  *          lower bound using the following equation.
  21.  *          @code lowerBound += (upperBound - lowerBound + 1) / 2 @endcode
  22.  *
  23.  * @param lowerBound           Lower bound to start searching from.
  24.  * @param upperBound           Upper bound to start searching from.
  25.  * @param instructions         A string of characters either lowerBoundDesignator or
  26.  *                             upperBoundDesignator.
  27.  * @param lowerBoundDesignator Lower bound instruction character.
  28.  * @param upperBoundDesignator Upper bound instruction character.
  29.  *
  30.  * @throws std::runtime_error in case of an invalid instruction string or
  31.  *         lowerBoundDesignator/upperBoundDesignator.
  32.  *
  33.  * @return The found value.
  34.  */
  35. static uint64_t splitFind(uint64_t           lowerBound,
  36.                           uint64_t           upperBound,
  37.                           const std::string& instructions,
  38.                           const char         lowerBoundDesignator,
  39.                           const char         upperBoundDesignator)
  40. {
  41.     for (uint64_t iChar = 0; iChar < instructions.size(); ++iChar) {
  42.         char c = instructions.c_str()[iChar];
  43.  
  44.         if (c == lowerBoundDesignator) {
  45.             lowerBound = lowerBound;
  46.             upperBound -= (upperBound - lowerBound + 1) / 2;
  47.         } else if (c == upperBoundDesignator) {
  48.             lowerBound += (upperBound - lowerBound + 1) / 2;
  49.             upperBound = upperBound;
  50.         } else {
  51.             throw std::runtime_error("Either invalid instruction string, or "
  52.                                      "lowerBoundDesignator/upperBoundDesignator!");
  53.         }
  54.     }
  55.  
  56.     return lowerBound == upperBound ? lowerBound
  57.                                     : throw std::runtime_error("Lower and upper bound do not "
  58.                                                                "match, possibly wrong instruction "
  59.                                                                "string!");
  60. }
  61.  
  62. /**
  63.  * @brief More optimized version of splitFind.
  64.  *
  65.  * @param instructions Instruction string.
  66.  * @param upperBoundDesignator Upper bound selector character.
  67.  *
  68.  * @return Calculated value.
  69.  */
  70. static uint64_t splitFindBetter(const std::string& instructions, const char upperBoundDesignator)
  71. {
  72.     uint64_t result = 0;
  73.     for (uint64_t iChar = 0; iChar < instructions.size(); ++iChar) {
  74.         result = ((result << 1) | (instructions.c_str()[iChar] == upperBoundDesignator));
  75.     }
  76.     return result;
  77. }
  78.  
  79. /**
  80.  * @brief Calculates the seat ID according to the following equation.
  81.  *        @code row * 8 + col @endcode
  82.  *
  83.  * @param row Seat row.
  84.  * @param col Seat column.
  85.  *
  86.  * @return The calculated seat ID
  87.  */
  88. static uint64_t calculateSeatId(uint64_t row, uint64_t col, uint64_t rowSeatIdMultiplier)
  89. {
  90.     return row * rowSeatIdMultiplier + col;
  91. }
  92.  
  93. int main()
  94. {
  95.     std::ifstream            file("../day5_bigboi3.txt");
  96.     std::vector<std::string> lines;
  97.  
  98.     std::string line0;
  99.     getline(file, line0);
  100.     file.seekg(0, std::ios::beg);
  101.  
  102.     uint64_t rowCharCount =
  103.         static_cast<uint64_t>(std::max(line0.find_last_of('F'), line0.find_last_of('B'))) + 1;
  104.  
  105.     const uint64_t rowSize  = std::pow(2, rowCharCount);
  106.     const uint64_t colSize  = std::pow(2, line0.size() - rowCharCount);
  107.     auto           seatGrid = new bool*[rowSize];
  108.     for (int iRow = 0; iRow < rowSize; ++iRow) {
  109.         seatGrid[iRow] = new bool[colSize];
  110.     }
  111.  
  112.     uint64_t seatIdMax = 0;
  113.  
  114.     std::string line;
  115.     while (getline(file, line)) {
  116.         uint64_t row    = splitFindBetter(line.substr(0, rowCharCount), 'B');
  117.         uint64_t col    = splitFindBetter(line.substr(rowCharCount), 'R');
  118.         uint64_t seatId = calculateSeatId(row, col, colSize);
  119.  
  120.         seatIdMax          = std::max(seatIdMax, seatId);
  121.         seatGrid[row][col] = true;
  122.  
  123.         //#ifndef NDEBUG
  124.         //        std::cout << "Row: " << row << " Column: " << col << " Seat ID: " << seatId << "\n";
  125.         //#endif
  126.     }
  127.  
  128.     std::cout << "Highest seat ID is: " << seatIdMax << std::endl;
  129.  
  130.     for (uint64_t iRow = 1; iRow < rowSize - 1; ++iRow) {
  131.         for (uint64_t iCol = 1; iCol < colSize - 1; ++iCol) {
  132.             if (!seatGrid[iRow][iCol] && seatGrid[iRow - 1][iCol] && seatGrid[iRow + 1][iCol] &&
  133.                 seatGrid[iRow][iCol - 1] && seatGrid[iRow][iCol + 1]) {
  134.                 std::cout << "Your seat ID is: " << calculateSeatId(iRow, iCol, colSize)
  135.                           << std::endl;
  136.             }
  137.         }
  138.     }
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment