day 25 simplify
parent
666aa2bcad
commit
5a27954c11
19
day25/sol.py
19
day25/sol.py
|
@ -8,33 +8,26 @@ def eval(num):
|
|||
def tosnafu(n):
|
||||
orig = n
|
||||
digits = []
|
||||
carry = 0
|
||||
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)
|
||||
carry, d = divmod(d+carry, 5)
|
||||
if d > 2:
|
||||
carry += 1
|
||||
d -= 5
|
||||
digits[i] = d
|
||||
digits.append(d)
|
||||
if carry:
|
||||
digits.append(carry)
|
||||
digits.reverse()
|
||||
|
||||
snafu = "".join("=-012"[d+2] for d in digits)
|
||||
print(n, digits, snafu)
|
||||
#print(n, digits, snafu)
|
||||
assert eval(snafu) == orig
|
||||
return snafu
|
||||
|
||||
|
||||
|
||||
|
||||
t = 0
|
||||
for line in open("input"):
|
||||
n = (eval(line.strip()))
|
||||
n = eval(line.strip())
|
||||
print(n)
|
||||
t += n
|
||||
|
||||
|
|
Loading…
Reference in New Issue