Skip to content

Commit

Permalink
2022/25 comment
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 25, 2022
1 parent 4b6537e commit 4382115
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions 2022/Day25/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ long SnafuToLong(string snafu) {
}

string LongToSnafu(long d) {
// Almost standard base conversion, but when dealing with digits 3 and 4
// we need to increment the higher decimal place and subtract 2 or 1.
// Almost standard base conversion, but when dealing with digits 3
// and 4 we need to increment the higher decimal place so that we have
// something to subtract 2 and 1 from.

var res = "";
while (d > 0) {
var digit = d % 5;
d /= 5;
switch (digit) {
switch (d % 5) {
case 0: res = '0' + res; break;
case 1: res = '1' + res; break;
case 2: res = '2' + res; break;
case 3: res = '=' + res; d++; break;
case 4: res = '-' + res; d++; break;
// add 5 and emit -2 because 3 = 5 -2
case 3: d+=5; res = '=' + res; break;
// add 5 and emit -1 because 4 = 5 -1
case 4: d+=5; res = '-' + res; break;
}
d /= 5;
}
return res;
}
Expand Down

0 comments on commit 4382115

Please sign in to comment.