44 lines
774 B
Python
44 lines
774 B
Python
|
def eval(num):
|
||
|
digits = ["=-012".index(c)-2 for c in num]
|
||
|
n = 0
|
||
|
for x in digits:
|
||
|
n = n*5 + x
|
||
|
return n
|
||
|
|
||
|
def tosnafu(n):
|
||
|
orig = n
|
||
|
digits = []
|
||
|
while n != 0:
|
||
|
n, d = divmod(n, 5)
|
||
|
digits.append(d)
|
||
|
print(digits)
|
||
|
|
||
|
carry = 0
|
||
|
for i in range(len(digits)):
|
||
|
d = digits[i] + carry
|
||
|
carry, d = divmod(d, 5)
|
||
|
if d > 2:
|
||
|
carry += 1
|
||
|
d -= 5
|
||
|
digits[i] = d
|
||
|
if carry:
|
||
|
digits.append(carry)
|
||
|
digits.reverse()
|
||
|
snafu = "".join("=-012"[d+2] for d in digits)
|
||
|
print(n, digits, snafu)
|
||
|
assert eval(snafu) == orig
|
||
|
return snafu
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
t = 0
|
||
|
for line in open("input"):
|
||
|
n = (eval(line.strip()))
|
||
|
print(n)
|
||
|
t += n
|
||
|
|
||
|
print(t)
|
||
|
print(tosnafu(t))
|
||
|
|