Ad

I prefer looking for overlaps on a min and max of the opposing range.

Code
Diff
  • static void normalize(int & num)
    {
        num %= 24;
    }
    
    static bool willTheyMeet(
            int startHour1,
            int endHour1,
            int startHour2,
            int endHour2) {
        normalize(startHour1);
        normalize(startHour2);
        normalize(endHour1);
        normalize(endHour2);
        
        return (startHour2 >= startHour1 && startHour2 <= endHour1) ||
                (endHour2 >= startHour1 && endHour2 <= endHour1) ||
                (startHour1 >= startHour2 && startHour1 <= endHour2) ||
                (endHour1 >= startHour2 && endHour1 <= endHour2);
    }
    • #include <cmath>
    • static float hourToRad(int hour)
    • static void normalize(int & num)
    • {
    • return (float) hour * (M_PI / 12.0);
    • };
    • num %= 24;
    • }
    • static bool willTheyMeet(
    • int startHour1,
    • int endHour1,
    • int startHour2,
    • int endHour2)
    • {
    • auto a1 = hourToRad(startHour1);
    • auto a2 = hourToRad(endHour1);
    • auto b1 = hourToRad(startHour2);
    • auto b2 = hourToRad(endHour2);
    • if (a1 == b2 || a2 == b1)
    • return false;
    • float da = (a2 - a1) / 2.0;
    • float db = (b2 - b1) / 2.0;
    • float ma = (a2 + a1) / 2.0;
    • float mb = (b2 + b1) / 2.0;
    • float cda = cos(da);
    • float cdb = cos(db);
    • return cos(ma - b1) >= cda ||
    • cos(ma - b2) >= cda ||
    • cos(mb - a1) >= cdb ||
    • cos(mb - a2) >= cdb;
    • };
    • int startHour1,
    • int endHour1,
    • int startHour2,
    • int endHour2) {
    • normalize(startHour1);
    • normalize(startHour2);
    • normalize(endHour1);
    • normalize(endHour2);
    • return (startHour2 >= startHour1 && startHour2 <= endHour1) ||
    • (endHour2 >= startHour1 && endHour2 <= endHour1) ||
    • (startHour1 >= startHour2 && startHour1 <= endHour2) ||
    • (endHour1 >= startHour2 && endHour1 <= endHour2);
    • }

While your lambdas are a cool language feature, they serve no purpose outside of being cool and they take the ability away from the compiler to optimize your code. If you take your test conditions and invert them, then use them as the escape conditions, you reduce your compiled assember instruction count from 104 instructions to 61 instructions, according to GodBolt. If you take out the individual ifs and use a single if with the original logic you had, not-inverted, then you get 51 instructions.

Check them out here: https://godbolt.org/z/R_Of7C

Code
Diff
  • #include <string>
    bool testPassword(std::string password) // 58 instructions
    {
        if (password.size() < 8) return false;
        if (password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos) return false;
        if (password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") == std::string::npos) return false;
        if (password.find_first_of("0123456789") == std::string::npos) return false;
        return true;
    }
    /* 51 instructions
    #include <string>
    bool testPassword(std::string password) 
    {
        if (password.size() >= 8 &&
            password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos &&
            password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos &&
            password.find_first_of("0123456789") != std::string::npos) 
            return true;
        return false;
    }
    */
    
    • #include <string>
    • bool testPassword(std::string password)
    • bool testPassword(std::string password) // 58 instructions
    • {
    • auto const has_length{[&]() { return password.size() >= 8; }};
    • auto const has_upper{[&]() { return password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos; }};
    • auto const has_special{[&]() { return password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos; }};
    • auto const has_digit{[&]() { return password.find_first_of("0123456789") != std::string::npos; }};
    • return has_length() && has_upper() && has_special() && has_digit();
    • }
    • if (password.size() < 8) return false;
    • if (password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos) return false;
    • if (password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") == std::string::npos) return false;
    • if (password.find_first_of("0123456789") == std::string::npos) return false;
    • return true;
    • }
    • /* 51 instructions
    • #include <string>
    • bool testPassword(std::string password)
    • {
    • if (password.size() >= 8 &&
    • password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos &&
    • password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos &&
    • password.find_first_of("0123456789") != std::string::npos)
    • return true;
    • return false;
    • }
    • */