adventofcode2022/day25/sol.py

37 lines
661 B
Python
Raw Normal View History

2022-12-25 05:16:39 +00:00
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 = []
2022-12-25 06:15:02 +00:00
carry = 0
2022-12-25 05:16:39 +00:00
while n != 0:
n, d = divmod(n, 5)
2022-12-25 06:15:02 +00:00
carry, d = divmod(d+carry, 5)
2022-12-25 05:16:39 +00:00
if d > 2:
carry += 1
d -= 5
2022-12-25 06:15:02 +00:00
digits.append(d)
2022-12-25 05:16:39 +00:00
if carry:
digits.append(carry)
digits.reverse()
2022-12-25 06:15:02 +00:00
2022-12-25 05:16:39 +00:00
snafu = "".join("=-012"[d+2] for d in digits)
2022-12-25 06:15:02 +00:00
#print(n, digits, snafu)
2022-12-25 05:16:39 +00:00
assert eval(snafu) == orig
return snafu
t = 0
for line in open("input"):
2022-12-25 06:15:02 +00:00
n = eval(line.strip())
2022-12-25 05:16:39 +00:00
print(n)
t += n
print(t)
print(tosnafu(t))