-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGalacticNumberSystem.cs
More file actions
60 lines (49 loc) · 1.39 KB
/
GalacticNumberSystem.cs
File metadata and controls
60 lines (49 loc) · 1.39 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MerchantsGuideToGalaxy
{
public class GalacticNumberSystem
{
private readonly Dictionary<string, RomanDigit> _aliases = new Dictionary<string, RomanDigit>();
public void SetAlias(string alias, RomanDigit digit)
{
_aliases[alias] = digit;
}
public RomanDigit GetAlias(string alias)
{
return _aliases[alias];
}
public int GetDecimalValue(string number)
{
int result = 0;
var digits = number.Split(' ').Select(GetAlias).ToArray();
if (digits.Length == 1)
{
return (int)digits.Single();
}
var index = 0;
var sum = 0;
while (index < digits.Length)
{
sum = 0;
var current = (int)digits[index];
var next = index < digits.Length - 1 ? (int)digits[index + 1] : 0;
if(next > current)
{
sum = next - current;
index += 2;
}
else
{
sum = current;
index++;
}
result += sum;
}
return result;
}
}
}