summaryrefslogtreecommitdiff
path: root/problem89.ijs
diff options
context:
space:
mode:
Diffstat (limited to 'problem89.ijs')
-rw-r--r--problem89.ijs20
1 files changed, 20 insertions, 0 deletions
diff --git a/problem89.ijs b/problem89.ijs
new file mode 100644
index 0000000..1c46234
--- /dev/null
+++ b/problem89.ijs
@@ -0,0 +1,20 @@
+romans =: 'IVXLCDM'
+decimals =: 1 5 10 50 100 500 1000
+romanToDecimal =: {{ +/ns*1,~_1+2*0<:2-/\ns=.decimals{~romans i.y }}
+decimalToRoman =: {{ ;groupToRoman each (0&~:#]) ds*|.10^i.#ds=.(10&#.^:_1) y }}
+groupToRoman =: {{
+ closest =. {./:|y-decimals
+ diff =. y - closest{decimals
+ if. (diff<0) *. -.(|diff) e. decimals do.
+ closest =. closest - 1
+ diff =. y - closest{decimals
+ end.
+ ch =. closest{romans
+ diff =. y - closest{decimals
+ sub =. {{ x,~groupToRoman y }}
+ keep =. [
+ add =. {{ x,groupToRoman y }}
+ ch sub`keep`add@.(>:*diff) |diff
+}}
+saves =: #-#&decimalToRoman&romanToDecimal
+problem89 =: +/;saves each 'b' freads 'p089_roman.txt'