about summary refs log tree commit diff
path: root/daily/305hard/number.pas
diff options
context:
space:
mode:
authorRaphael McSinyx <vn.mcsinyx@gmail.com>2017-03-14 10:23:43 +0700
committerRaphael McSinyx <vn.mcsinyx@gmail.com>2017-03-14 10:23:43 +0700
commit69a50f40bea8f04c2d2b1715dad5ea8c7d6ef507 (patch)
treef51905a868a94fa6eac79f5a6d6ec5aceb139f42 /daily/305hard/number.pas
parent2468c60b00b200b24a383ecc0fdc5d3cf8721b9f (diff)
downloadcp-69a50f40bea8f04c2d2b1715dad5ea8c7d6ef507.tar.gz
Add /r/dailyprogrammer Challenge #305
Diffstat (limited to 'daily/305hard/number.pas')
-rw-r--r--daily/305hard/number.pas54
1 files changed, 54 insertions, 0 deletions
diff --git a/daily/305hard/number.pas b/daily/305hard/number.pas
new file mode 100644
index 0000000..6a2d6e5
--- /dev/null
+++ b/daily/305hard/number.pas
@@ -0,0 +1,54 @@
+var
+  a: array[0..15] of byte;
+
+
+function factorial(n: byte): int64;
+  begin
+    if n < 2 then
+      exit(1);
+    factorial := factorial(pred(n)) * n
+  end;
+
+
+function number(
+  a: array of byte;
+  depth: byte
+): int64;
+
+  var
+    i, sum: byte;
+    b: array[0..9] of byte;
+
+  begin
+    if depth = 15 then
+      begin
+        sum := 0;
+        for i := 1 to 15 do
+          inc(sum, a[i]);
+        if sum <> 69 then
+          exit(0);
+        number := 1307674368000; (* factorial(15) *)
+        for i := 0 to 9 do
+          b[i] := 0;
+        for i := 1 to 15 do
+          inc(b[a[i]]);
+        for i := 0 to 9 do
+          number := number div factorial(b[i]);
+        exit
+      end;
+
+    number := 0;
+    i := a[depth];
+    inc(depth);
+    repeat
+      a[depth] := i;
+      number := number + number(a, depth);
+      inc(i)
+    until i = 10
+  end;
+
+
+begin
+  a[0] := 0;
+  writeln(number(a, 0))
+end.