diff options
author | Vincent Ambo <tazjin@google.com> | 2020-05-11T01·39+0100 |
---|---|---|
committer | Vincent Ambo <tazjin@google.com> | 2020-05-11T01·39+0100 |
commit | 9628daf41331958e5cb217490bfb16a600af6dbb (patch) | |
tree | 718fb5f5c1a91d91170a31b4fa9b5776c71cf3be /fun/dt | |
parent | 98473d21ca6d493b70f586c08add7e37250c6b2b (diff) |
feat(fun/dt): Support arbitrary word counts r/713
Diffstat (limited to 'fun/dt')
-rw-r--r-- | fun/dt/dt.cc | 73 |
1 files changed, 47 insertions, 26 deletions
diff --git a/fun/dt/dt.cc b/fun/dt/dt.cc index 895dc34051da..3c34dbbd1909 100644 --- a/fun/dt/dt.cc +++ b/fun/dt/dt.cc @@ -1,56 +1,77 @@ #include <iostream> +#include <vector> #include "absl/flags/flag.h" #include "absl/flags/parse.h" #include "absl/hash/hash.h" -#include "absl/time/time.h" -#include "absl/time/clock.h" #include "absl/strings/str_cat.h" +#include "absl/time/clock.h" +#include "absl/time/time.h" +#include "absl/types/optional.h" #include "farmhash.h" -ABSL_FLAG(std::string, one, "", "first word"); -ABSL_FLAG(std::string, two, "", "second word"); +ABSL_FLAG(std::vector<std::string>, word, {}, "words to use"); ABSL_FLAG(int, range, 100, "operating range"); -void which(std::string one, std::string two) { - if (util::Fingerprint64(one) > util::Fingerprint64(two)) { - std::cout << one << std::endl; - } else { - std::cout << two << std::endl; +struct Result { + std::string a; + absl::optional<std::string> p; +}; + +std::string which(const std::vector<std::string>& words) { + uint64_t fp; + std::string word; + + for (const auto& w : words) { + auto nfp = util::Fingerprint64(w); + if (nfp > fp) { + fp = nfp; + word = w; + } } + + return word; } -std::string decide(std::string one, std::string two) { - auto date = absl::FormatTime("%Y%m%d", absl::Now(), absl::UTCTimeZone()); - auto base = util::Fingerprint64(absl::StrCat(date, one, two)) - % (absl::GetFlag(FLAGS_range) + 1); +Result decide(const std::vector<std::string>& words) { + auto input = absl::FormatTime("%Y%m%d", absl::Now(), absl::UTCTimeZone()); + for (const auto& w : words) { + input += w; + } + + auto base = util::Fingerprint64(input) % (absl::GetFlag(FLAGS_range) + 1); + + Result result = { "nope" }; if (base % 10 == 0) { - return "c2"; + result.a = "ca"; } else if (base % 9 == 0) { - which(one, two); - return "c1"; + result.a = "c1"; + result.p = which(words); } else if (base % 8 == 0) { - return "e2"; + result.a = "ea"; } else if (base % 7 == 0) { - which(one, two); - return "e1"; + result.a = "e1"; + result.p = which(words); } else if (base % 3 == 0) { - return "skip"; + result.a = "skip"; } - return "nope"; + return result; } int main(int argc, char *argv[]) { absl::ParseCommandLine(argc, argv); - auto one = absl::GetFlag(FLAGS_one); - auto two = absl::GetFlag(FLAGS_two); - if (one.empty() || two.empty()) { - std::cerr << "both are required!" << std::endl; + auto words = absl::GetFlag(FLAGS_word); + if (words.size() < 2) { + std::cerr << "needs at least two!" << std::endl; return 1; } - std::cout << decide(one, two) << std::endl; + auto result = decide(words); + std::cout << result.a + << (result.p.has_value() ? absl::StrCat(" ", "(", result.p.value(), ")") + : "") + << std::endl; } |