commit a7280c44a32222d4ddcdd98965ffec73e7eb0a56 Author: David Vereb Date: Thu Dec 3 11:43:51 2020 -0500 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54ce77b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +a.out diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..96baac6 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +a.out: main.cpp + clang++ -O0 -g -std=c++14 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..5eea82a --- /dev/null +++ b/main.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include // atoi + +class Rate +{ +public: + Rate(int rate) + : base_rate(rate), min_rate(rate) + { } + + /* + * This is the rate given to you by the user + */ + int BaseRate() const { return base_rate; } + + /* + * This is the least common multiple when compared to all the other rates + */ + int MinimumRate() const { return min_rate; } + + /* + * This tells you how many you'd need to build to achieve the minimum rate + */ + int QuantityToBuild() const + { + // TODO(dev): assert !(min_rate % rate), otherwise this shit's broken or not yet calculated + return min_rate / base_rate; + } + + /* + * This is used to step up to the next rate + */ + void Increment() { min_rate += base_rate; } + +private: + const int base_rate; + + int min_rate; // needs to be incremented/calculated in the program loop +}; + +int main(int argc, char *argv[]) +{ + if(argc < 3) + { + std::cout << "Usage: ./" << argv[0] << " rate1 rate2 [...] rateN" << std::endl; + return -1; + } + + std::vector rates; + + // Add all command line parameters (rates) to the vector: + for(auto i = 1; i < argc; ++i) + rates.push_back(Rate(atoi(argv[i]))); + + // NOTE(dev): Run through all (2+) rates to determine the least common multiple. + // Increment each rate until they all come out to the same value + bool difference_found; + do + { + difference_found = false; + for(auto &rate : rates) + { + for(auto &other : rates) // sure it checks against itself, but this isn't optimized sooo + { + if(rate.MinimumRate() < other.MinimumRate()) + { + difference_found = true; + while(rate.MinimumRate() < other.MinimumRate()) + rate.Increment(); + } + } + } + } while(difference_found); + + for(auto rate : rates) + std::cout << rate.QuantityToBuild() << " * " << rate.BaseRate() << " == " + << rate.MinimumRate() << std::endl; + + return 0; +}