Skip to content

Commit 4382115

Browse files
committed
2022/25 comment
1 parent 4b6537e commit 4382115

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

2022/Day25/Solution.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@ long SnafuToLong(string snafu) {
3131
}
3232

3333
string LongToSnafu(long d) {
34-
// Almost standard base conversion, but when dealing with digits 3 and 4
35-
// we need to increment the higher decimal place and subtract 2 or 1.
34+
// Almost standard base conversion, but when dealing with digits 3
35+
// and 4 we need to increment the higher decimal place so that we have
36+
// something to subtract 2 and 1 from.
37+
3638
var res = "";
3739
while (d > 0) {
38-
var digit = d % 5;
39-
d /= 5;
40-
switch (digit) {
40+
switch (d % 5) {
4141
case 0: res = '0' + res; break;
4242
case 1: res = '1' + res; break;
4343
case 2: res = '2' + res; break;
44-
case 3: res = '=' + res; d++; break;
45-
case 4: res = '-' + res; d++; break;
44+
// add 5 and emit -2 because 3 = 5 -2
45+
case 3: d+=5; res = '=' + res; break;
46+
// add 5 and emit -1 because 4 = 5 -1
47+
case 4: d+=5; res = '-' + res; break;
4648
}
49+
d /= 5;
4750
}
4851
return res;
4952
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy