-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25-snafuNums.chpl
59 lines (52 loc) · 1.35 KB
/
day25-snafuNums.chpl
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
// day25-snafuNums
use IO, Map;
iter readLines() {
var line : string;
while readLine(line) {
yield line.strip();
}
}
var numArray = readLines();
writeln(numArray);
var snafuToDecimal : map(string,int);
snafuToDecimal["1"] = 1;
snafuToDecimal["2"] = 2;
snafuToDecimal["0"] = 0;
snafuToDecimal["-"] = -1;
snafuToDecimal["="] = -2;
// converting snafuToDecimal
var decVal : [numArray.domain] int;
for (idx,str) in zip(numArray.domain,numArray) {
decVal[idx] = 0;
var placeVal = 1;
for i in 0..#str.size by -1 {
decVal[idx] += placeVal*snafuToDecimal[str[i]];
placeVal *= 5;
}
writeln("decVal[idx] = ", decVal[idx]);
}
const sum = + reduce decVal;
writeln("sum = ", + reduce decVal);
// converting decimal to snafu
//for n in decVal { // this was for testing
// FIXME: need to refactor most of below into a function
// and turn into a do while loop
var str = "";
var num = sum; // <====================
var numMod5 = num%5;
var numDiv5 = num/5;
while numDiv5>0 || numMod5>0 {
select numMod5 {
when 2 { str = "2" + str; }
when 1 { str = "1" + str; }
when 0 { str = "0" + str; }
when 3 { str = "=" + str; num += 5; }
when 4 { str = "-" + str; num += 5; }
otherwise { writeln("ERROR"); }
}
num = (num - numMod5)/5;
numMod5 = num%5;
numDiv5 = num/5;
}
writeln("str = ", str);
//}