day 25 simplify

main
magical 2022-12-24 22:15:02 -08:00
parent 666aa2bcad
commit 5a27954c11
1 changed files with 6 additions and 13 deletions

View File

@ -8,33 +8,26 @@ def eval(num):
def tosnafu(n): def tosnafu(n):
orig = n orig = n
digits = [] digits = []
carry = 0
while n != 0: while n != 0:
n, d = divmod(n, 5) n, d = divmod(n, 5)
digits.append(d) carry, d = divmod(d+carry, 5)
print(digits)
carry = 0
for i in range(len(digits)):
d = digits[i] + carry
carry, d = divmod(d, 5)
if d > 2: if d > 2:
carry += 1 carry += 1
d -= 5 d -= 5
digits[i] = d digits.append(d)
if carry: if carry:
digits.append(carry) digits.append(carry)
digits.reverse() digits.reverse()
snafu = "".join("=-012"[d+2] for d in digits) snafu = "".join("=-012"[d+2] for d in digits)
print(n, digits, snafu) #print(n, digits, snafu)
assert eval(snafu) == orig assert eval(snafu) == orig
return snafu return snafu
t = 0 t = 0
for line in open("input"): for line in open("input"):
n = (eval(line.strip())) n = eval(line.strip())
print(n) print(n)
t += n t += n