-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb089.jl
67 lines (54 loc) · 1.16 KB
/
pb089.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Problem089
const romanvalues = (
("M", 1000),
("CM", 900),
("D", 500),
("CD", 400),
("C", 100),
("XC", 90),
("L", 50),
("XL", 40),
("X", 10),
("IX", 9),
("V", 5),
("IV", 4),
("I", 1),
)
"""
problem089()
Problem 089 of Project Euler.
https://projecteuler.net/problem=089
"""
function problem089(filename="txt/pb089.txt")
sum(length(rm) - length(decimal_to_roman(roman_to_decimal(rm))) for rm in readlines(filename))
end
function roman_to_decimal(r)
r = replace(r,
"IV" => "IIII",
"IX" => "VIIII",
"XL" => "XXXX",
"XC" => "LXXXX",
"CD" => "CCCC",
"CM" => "DCCCC"
)
n = 1000count(isequal('M'), r)
n += 500count(isequal('D'), r)
n += 100count(isequal('C'), r)
n += 50count(isequal('L'), r)
n += 10count(isequal('X'), r)
n += 5count(isequal('V'), r)
n += count(isequal('I'), r)
return n
end
function decimal_to_roman(n)
rm = ""
for (r, x) in romanvalues
rm *= repeat(r, n ÷ x)
n -= x * (n ÷ x)
end
return rm
end
export problem089
end # module Problem089
using .Problem089
export problem089