about summary refs log tree commit diff
path: root/fun
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-05-08T17·55+0100
committerVincent Ambo <tazjin@google.com>2020-05-08T17·55+0100
commite33627f960c767c7408a4885a0061ade2703c9f9 (patch)
tree781e936efd78f7856ef33dcedb5b98d7789c9fd8 /fun
parent28a36a2b70e31f9b8bf90085cf1b70fe038e9ff6 (diff)
feat(fun/dt): Implement useful utility r/707
Diffstat (limited to 'fun')
-rw-r--r--fun/dt/CMakeLists.txt16
-rw-r--r--fun/dt/README.md11
-rw-r--r--fun/dt/default.nix14
-rw-r--r--fun/dt/dt.cc56
4 files changed, 97 insertions, 0 deletions
diff --git a/fun/dt/CMakeLists.txt b/fun/dt/CMakeLists.txt
new file mode 100644
index 0000000000..85b659fea8
--- /dev/null
+++ b/fun/dt/CMakeLists.txt
@@ -0,0 +1,16 @@
+# -*- mode: cmake; -*-
+cmake_minimum_required(VERSION 3.16)
+project(dt)
+add_executable(dt dt.cc)
+find_package(absl REQUIRED)
+
+target_link_libraries(dt
+  absl::flags
+  absl::flags_parse
+  absl::hash
+  absl::time
+  absl::strings
+  farmhash
+)
+
+install(TARGETS dt DESTINATION bin)
diff --git a/fun/dt/README.md b/fun/dt/README.md
new file mode 100644
index 0000000000..ee43d56064
--- /dev/null
+++ b/fun/dt/README.md
@@ -0,0 +1,11 @@
+dt
+==
+
+It's got a purpose.
+
+## Usage:
+
+```
+nix-build -E '(import (builtins.fetchGit "https://git.tazj.in/") {}).fun.dt'
+./result/bin/dt --one ... --two ...
+```
diff --git a/fun/dt/default.nix b/fun/dt/default.nix
new file mode 100644
index 0000000000..4dbf00ede1
--- /dev/null
+++ b/fun/dt/default.nix
@@ -0,0 +1,14 @@
+{ depot, pkgs, ... }:
+
+let
+  stdenv = with pkgs; overrideCC clangStdenv clang_9;
+  abseil-cpp = pkgs.abseil-cpp.override { inherit stdenv; };
+in stdenv.mkDerivation {
+  name = "dt";
+  src = ./.;
+  nativeBuildInputs = [ pkgs.cmake ];
+  buildInputs = with pkgs; [
+    abseil-cpp
+    farmhash
+  ];
+}
diff --git a/fun/dt/dt.cc b/fun/dt/dt.cc
new file mode 100644
index 0000000000..895dc34051
--- /dev/null
+++ b/fun/dt/dt.cc
@@ -0,0 +1,56 @@
+#include <iostream>
+
+#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 "farmhash.h"
+
+ABSL_FLAG(std::string, one, "", "first word");
+ABSL_FLAG(std::string, two, "", "second word");
+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;
+  }
+}
+
+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);
+
+  if (base % 10 == 0) {
+    return "c2";
+  } else if (base % 9 == 0) {
+    which(one, two);
+    return "c1";
+  } else if (base % 8 == 0) {
+    return "e2";
+  } else if (base % 7 == 0) {
+    which(one, two);
+    return "e1";
+  } else if (base % 3 == 0) {
+    return "skip";
+  }
+
+  return "nope";
+}
+
+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;
+    return 1;
+  }
+
+  std::cout << decide(one, two) << std::endl;
+}