about summary refs log tree commit diff
path: root/daily/305easy
diff options
context:
space:
mode:
Diffstat (limited to 'daily/305easy')
-rw-r--r--daily/305easy/README.md50
-rw-r--r--daily/305easy/permbase.pas50
2 files changed, 100 insertions, 0 deletions
diff --git a/daily/305easy/README.md b/daily/305easy/README.md
new file mode 100644
index 0000000..0f8876b
--- /dev/null
+++ b/daily/305easy/README.md
@@ -0,0 +1,50 @@
+# [[2017-03-06] Challenge #305 [Easy] Permutation base](https://www.reddit.com/r/dailyprogrammer/comments/5xu7sz/20170306_challenge_305_easy_permutation_base/)
+
+There may be an actual name to this base system (let us/me know in comments),
+and there is a math insight that makes this problem even easier, but it is
+still pretty easy with no math insight.
+
+For *permutation base 2*, the indexes and values start with:
+
+| index | value |
+| :---: | ----: |
+|   0   |    0  |
+|   1   |    1  |
+|   2   |   00  |
+|   3   |   01  |
+|   4   |   10  |
+|   5   |   11  |
+|   6   |  000  |
+|   7   |  001  |
+|   8   |  010  |
+|   9   |  011  |
+
+## Sample challenge
+
+What is the base-value for index `54`?
+
+What is the index-value for base `111000111`?
+
+## Example
+
+    permbase2 54
+    1 1 0 0 0
+
+    permbase2 inv 1 1 1 0 0 0 1 1 1
+    965
+
+## Challenge index inputs (some are 64-bit+ inputs)
+
+    234234234
+    234234234234234
+    234234234234234234234234
+
+## Challenge value inputs
+
+    000111000111111000111111000111111000111
+    11111111000111000111111000111111000111111000111
+
+## Bonus
+
+Extend the function to work with any base. Base 10 index value `10` is `00`.
+Index value `109` is `99`
diff --git a/daily/305easy/permbase.pas b/daily/305easy/permbase.pas
new file mode 100644
index 0000000..5513a77
--- /dev/null
+++ b/daily/305easy/permbase.pas
@@ -0,0 +1,50 @@
+uses math, strutils, sysutils;
+
+var
+  inv: string;
+  b: byte;
+  n: string;
+
+
+function permbase(
+  base: byte;
+  index: longint
+): string;
+
+  var
+    i: byte = 1;
+
+  begin
+    while base ** i <= index do
+      begin
+        index := index - base ** i;
+        inc(i)
+      end;
+    permbase := dec2numb(index, i, base)
+  end;
+
+
+function invpermbase(
+  base: byte;
+  value: string
+): longint;
+
+  begin
+    invpermbase := (base ** length(value) - base) div (base - 1) +
+                   numb2dec(value, base)
+  end;
+
+begin
+  (*
+   * Input format:
+   * <invert?>
+   * <base> <value>
+   *)
+  readln(inv);
+  readln(b, n);
+  n := delspace(n);
+  if strtobool(inv) then
+    writeln(invpermbase(b, n))
+  else
+    writeln(permbase(b, strtoint(n)))
+end.