1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import math
- lines = [line.strip() for line in open("25.input")]
- def to_dec(num):
- total = 0
- for i, c in enumerate(reversed(num)):
- if c in ["0", "1", "2"]:
- total += (5 ** i) * int(c)
- elif c == "-":
- total -= (5 ** i)
- elif c == "=":
- total -= (5 ** i) * 2
- return total
- def from_dec(num):
- if num == 1:
- return "1"
- rem = num
- exp = 0
- while num > to_dec("2" + "2" * exp):
- exp += 1
- result = []
- while exp >= 0:
- if rem == 0:
- result.append("0")
- elif rem >= to_dec("2" + "=" * exp):
- result.append("2")
- rem -= (5 ** exp) * 2
- elif rem >= to_dec("1" + "=" * exp):
- result.append("1")
- rem -= (5 ** exp) * 1
- elif rem <= to_dec("=" + "2" * exp):
- result.append("=")
- rem += (5 ** exp) * 2
- elif rem <= to_dec("-" + "2" * exp):
- result.append("-")
- rem += (5 ** exp)
- else:
- result.append("0")
- #print("res=", "".join(result), ", rem=", rem)
- exp -= 1
- return "".join(result)
- result = sum(to_dec(line) for line in lines)
- print(result, "=", from_dec(result))
|