Guest User

--- Day 5: Binary Boarding ---

a guest
Dec 5th, 2020
98
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4.  
  5. int GetValBSP(int min, int max, std::string pass, int i, int m)
  6. {
  7.     if (max - min <= 1)
  8.         return (pass[i] == 'F' || pass[i] == 'L') ? min : max;
  9.    
  10.     if (pass[i] == 'F' || pass[i] == 'L')
  11.         return GetValBSP(min, ((max - min) / 2) + min, pass, std::min(i+1, m), m);
  12.     else
  13.         return GetValBSP(((min - max) / 2) + max, max, pass, std::min(i+1, m), m);
  14. }
  15.  
  16. int main()
  17. {
  18.    unsigned int highestID = 0;
  19.    
  20.    std::string pass;
  21.    
  22.    bool seatTaken[1024];
  23.    
  24.    while (getline(std::cin, pass))
  25.    {
  26.        unsigned int row = GetValBSP(0, 127, pass, 0, 6);
  27.        unsigned int column = GetValBSP(0, 7, pass, 7, 9);
  28.        unsigned int id = row * 8 + column;
  29.        
  30.        std::cout << row << " * 8 + " << column << " = " << id << std::endl;
  31.        
  32.        seatTaken[id] = true;
  33.        
  34.        highestID = std::max(highestID, id);
  35.    }
  36.    
  37.    std::cout << "Highest ID: " << highestID << std::endl;
  38.    
  39.    for (int i = 1 * 8; i < 126 * 8; i++)
  40.    {
  41.        if (seatTaken[i] == false)
  42.         std::cout << "Seat Free ID: " << i << std::endl;
  43.    }
  44.    
  45.    return 0;
  46. }
RAW Paste Data