25_snafu.py 440 B

12345678910111213141516
  1. #!/usr/bin/env python3
  2. import sys
  3. def to_decimal(snafu):
  4. return sum(5 ** i * ('=-012'.index(digit) - 2)
  5. for i, digit in enumerate(reversed(snafu)))
  6. def to_snafu(decimal):
  7. digits = []
  8. while decimal:
  9. decimal, rem = divmod(decimal, 5)
  10. decimal += rem > 2
  11. digits.append('012=-'[rem])
  12. return ''.join(reversed(digits))
  13. print(to_snafu(sum(to_decimal(line.rstrip()) for line in sys.stdin)))