day 25, base conversion
This commit is contained in:
parent
f4d71daadf
commit
5d4d2f9f38
|
@ -0,0 +1,21 @@
|
|||
import sys
|
||||
|
||||
def snafu2dec(s:str)->int:
|
||||
D = {'0': 0, '1':1, '2':2, '-':-1, '=':-2 }
|
||||
n = 0
|
||||
for (i,c) in enumerate(reversed(s)):
|
||||
n += 5**i * D[c]
|
||||
return n
|
||||
|
||||
def dec2snafu(n:int) -> str:
|
||||
d = "=-012"
|
||||
l = []
|
||||
while n>0:
|
||||
r=(n+2)%5-2
|
||||
l.append(d[r+2])
|
||||
n = (n-r) // 5
|
||||
return "".join(reversed(l))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(dec2snafu(sum(snafu2dec(l) for l in sys.stdin.read().splitlines())))
|
Loading…
Reference in New Issue