diff options
author | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2021-09-27 14:30:01 +0200 |
---|---|---|
committer | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2021-09-27 14:30:01 +0200 |
commit | 9a71423e741578de899e24466cdff08028f0f304 (patch) | |
tree | 954ee4f0843cee5f199da57a4d829ba400991a21 /problem89.ijs | |
parent | 898a95e59780dbee58c0a08ba8594b5e33953ccb (diff) |
Problem 89
Diffstat (limited to 'problem89.ijs')
-rw-r--r-- | problem89.ijs | 20 |
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' |