···11+import fileinput
22+33+SNAFU_DIGITS = {
44+ '2': 2,
55+ '1': 1,
66+ '0': 0,
77+ '-': -1,
88+ '=': -2,
99+}
1010+REVERSE_DIGITS = {v: k for k, v in SNAFU_DIGITS.items()}
1111+1212+1313+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]
2121+2222+2323+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
2828+2929+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))