···11import fileinput
22+from collections import Counter
33+from itertools import zip_longest
2435SNAFU_DIGITS = {
46 '2': 2,
···1012REVERSE_DIGITS = {v: k for k, v in SNAFU_DIGITS.items()}
111312141313-def decimal_to_base(n, b):
1414- if n == 0:
1515- return [0]
1616- digits = []
1717- while n:
1818- digits.append(int(n % b))
1919- n //= b
2020- return digits[::-1]
1515+# Read problem input and solve problem.
1616+nums = [line.strip() for line in fileinput.input()]
1717+remainder = Counter()
1818+part_1 = ''
21192020+for i, place in enumerate(zip_longest(*(reversed(n) for n in nums), fillvalue='0')):
2121+ count = sum(SNAFU_DIGITS[c] for c in place) + remainder[i]
2222+ while not -2 <= count <= 2:
2323+ if count < 0:
2424+ remainder[i+1] -= 1
2525+ count += 5
2626+ else:
2727+ remainder[i+1] += 1
2828+ count -= 5
22292323-def snafu_to_decimal(s):
2424- n = 0
2525- for i, c in enumerate(reversed(s)):
2626- n += SNAFU_DIGITS[c] * (5 ** i)
2727- return n
3030+ part_1 = REVERSE_DIGITS[count] + part_1
28312929-3030-def decimal_to_snafu(n):
3131- quinary_digits = decimal_to_base(n, 5)
3232- snafu_digits = []
3333-3434- # Iterate in reverse order through the base-5 digits; if a number larger
3535- # than 2 is found, add 1 to the next "place" and take the remainder.
3636- for i, n in enumerate(reversed(quinary_digits)):
3737- if n >= 3:
3838- quinary_digits[-(i+2)] += 1
3939- n -= 5
4040- snafu_digits = [REVERSE_DIGITS[n]] + snafu_digits
4141-4242- return ''.join(snafu_digits)
4343-4444-4545-# Read problem input and solve problem.
4646-total = sum(snafu_to_decimal(line.strip()) for line in fileinput.input())
4747-print("Part 1:", decimal_to_snafu(total))
3232+print("Part 1:", part_1)