Skip to main content
Loading...

More C++ Posts

#include <iostream>
#include <vector>
#include <limits>

#define DEBUG_TRIAL false

class Trial{
    public:
        const size_t HEIGHT;
        std::string record;
        
        //Breaking height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
        //Eggs is the eggs remaining.
        //Start is the bottom floor.
        //End is one above the top floor.
        const size_t BREAKING_HEIGHT;
        size_t eggs;
        size_t start;
        size_t end;
        
        
        size_t floorsLeft(){
            return (end-start);
        }
        
        size_t middle(){
            return start + (floorsLeft()/2UL);
        }
        
        size_t drops = 0;
        Trial(const size_t BREAKING_HEIGHT, size_t eggs, size_t start, size_t end): BREAKING_HEIGHT(BREAKING_HEIGHT), eggs(eggs), start(start), end(end), HEIGHT(end), record(end, '_'){
            record[BREAKING_HEIGHT] = 'B'; //Marking the breaking point
        }
        
        bool foundAnswer(){
            return ((record[0] == 'X') || (record.find("OX")!=std::string::npos));
        }
        
        //returns true if the egg broke.
        //height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
        bool drop(size_t height){
            
            #if DEBUG_TRIAL
                std::cout << "Start: " << start << ". End: " << end << ". Floors Left: " << floorsLeft() << ". Middle Index: " << middle() << std::endl;
            #endif
            
            drops++;
            bool cracked = height >= BREAKING_HEIGHT;
            if(cracked) --eggs;
            
            //Update the record
            record[height] = (height >= BREAKING_HEIGHT)? 'X' : 'O';
            
            #if DEBUG_TRIAL
                //Print the record
                std::cout << record << std::endl;
            #endif
        
            return cracked;
        }
        
        size_t nowWhat(){
            if(foundAnswer()){
                return drops;
            }else if(eggs <= 0){ //Ran out of eggs
                throw "Algorithm failed! No more eggs!";
                return 1UL;
            }else if(eggs > 1){
                return wrecklessSearch();
            }else{
                return safeSearch();
            }
        }
        
        size_t safeSearch(){
            if(drop(start)){
                --end;
            }else{
                ++start;
            }
            
            return nowWhat();
        }
        
        size_t wrecklessSearch(){
            //If the egg breaks
            if(drop(middle())){
                end -= (floorsLeft()/2UL);
            }else{ //egg doesn't crack
                start += (floorsLeft()/2UL);
            }
            
            return nowWhat();
        }
        
        //returns the amount of drops needed to find the answer
        size_t search(){
            return nowWhat();
        }
};

//Height is the height of the building in floors.
//Breaking height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
//Eggs is the eggs given.
//returns the amount of drops needed to find the answer
size_t search(const size_t height, const size_t BREAKING_HEIGHT, size_t eggs){
    Trial trial(BREAKING_HEIGHT, eggs, 0, height);
    return trial.search();
}

class TrialStats {
    public:
        size_t min = std::numeric_limits<size_t>::max();
        size_t max = 0;
        double mean = -1.0;
        
        void printStats(){
            // Print the results
            std::cout << "Minimum drops: " << min << std::endl;
            std::cout << "Maximum drops: " << max << std::endl;
            std::cout << "Mean drops: " << mean << std::endl;
        }
};

//Benchmarks all the possible breaking points of a single building height with a number of eggs.
TrialStats trial(const size_t HEIGHT, const size_t eggs){
    
    TrialStats stats;
    int totaldrops = 0;

    //Test every possible breaking point
    //Breaking height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
    for (int breakingHeight = 0; breakingHeight < HEIGHT; ++breakingHeight) {
        size_t drops = search(HEIGHT, breakingHeight, eggs);

        stats.min = std::min(stats.min, drops);
        stats.max = std::max(stats.max, drops);
        totaldrops += drops;
    }

    // Calculate the mean number of drops
    stats.mean = static_cast<double>(totaldrops) / HEIGHT;
    
    return stats;
}

//Benchmarks a single building height from 1 egg to MAX_EGGS
void testTower(const size_t height, const size_t MAX_EGGS){
    //Drop every amount of eggs that you'd need.
    for (int eggs = 1; eggs <= MAX_EGGS; ++eggs) {
        std::cout << "Building height: " << height << ". Num eggs: " << eggs << std::endl;
        
        TrialStats stats = trial(height, eggs);
        stats.printStats();
        
        std::cout << std::endl << std::endl;
    }
}

//Benchmarks all buildings from 0 to MAX_HEIGHT
void benchmark(const size_t MAX_HEIGHT){
    const size_t MAX_EGGS = 2;
    //Test every building
    for (size_t height = 1; height <= MAX_HEIGHT; ++height) {
        testTower(height, std::min(height, MAX_EGGS));
    }
}

int main() {
    constexpr size_t MAX_HEIGHT = 36;
    
    benchmark(MAX_HEIGHT);

    return 0;
}
#define NUM_BITS 8

#include <iostream>


struct Number{
    int num : NUM_BITS;
    Number(){}
    Number(const int& bruh){
        num = bruh;
    }
    operator int() const { return num; }
    Number& operator=(const int& bruh){
        num = bruh;
        return (*this);
    }
};

using namespace std;

bool isNegative(const int& num){
    //This gets the bitwise and of num and 10000000000000000000000000000000
    //This implicit casts to bool, which means (num & (1 << 31)) != 0
    return (num & (1 << 31));
}

void printBinaryNumber(const int& num, const int numBits){
    for(int i = numBits; i > 0; --i){
        //8..1
        int bitMask = 1 << (i-1);
        if(num & bitMask){ //Test the bit
            cout << '1';
        }else{
            cout << '0';
        }
    }
}

void printCarryBits(const int& a, const int& b, const int numBits){
    int answer = 0;
    
    bool carry = false;
    for(int i = 0; i < numBits; ++i){
        //8..1
        int bitMask = 1 << i;
        bool aBit = a & bitMask;
        bool bBit = b & bitMask;
        if(aBit && bBit || aBit && carry || bBit && carry){ //Carry bit is true next
            if(carry)
                answer |= bitMask;
            carry = true;
        }else{
            if(carry)
                answer |= bitMask;
            carry = false;
        }
    }
    printBinaryNumber(answer, 8);
}


void printBorrowBits(const int& a, const int& b, const int numBits){
    int answer = 0;
    
    bool carry = false;
    for(int i = 0; i < numBits; ++i){
        //8..1
        int bitMask = 1 << i;
        bool aBit = a & bitMask;
        bool bBit = b & bitMask;
        if((!(aBit ^ carry)) && bBit){ //Carry bit is true next
            if(carry)
                answer |= bitMask;
            carry = true;
        }else{
            if(carry)
                answer |= bitMask;
            carry = false;
        }
    }
    printBinaryNumber(answer, 8);
}

void doProblem(const int& a, const int& b, const char& sign, const int& result, const int& numBits){
    if(sign == '+'){
        cout << ' '; printCarryBits(a, b, numBits); cout << endl;
    }else{
        cout << ' '; printBorrowBits(a, b, numBits); cout << endl;
    }
    cout << ' '; printBinaryNumber(a, numBits); cout << endl;
    cout << sign; printBinaryNumber(b, numBits); cout << endl;
    cout << "----------" << endl;
    cout << ""; printBinaryNumber(result, numBits + 1); cout << " = " << result;
    cout << endl;
}

int main(){
    
    Number a = 0b110;
    Number b = 0b011;
    cout<< a << endl << b << endl;
    
    doProblem(a, b, '+', a + b, NUM_BITS);
    doProblem(a, b, '-', a - b, NUM_BITS);
    doProblem(-a, b, '+', -a + b, NUM_BITS);
    doProblem(a, b, '-', -a - b, NUM_BITS);
    

    return 0;
}