day 25, base conversion

This commit is contained in:
setop 2022-12-29 12:09:51 +01:00
parent f4d71daadf
commit 5d4d2f9f38
1 changed files with 21 additions and 0 deletions

21
d25/part1.py Normal file
View File

@ -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())))