-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd25_alt.py
27 lines (22 loc) · 823 Bytes
/
d25_alt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from itertools import zip_longest, accumulate
from collections import deque
data = [x.strip('\n') for x in open("i25.txt").readlines()]
def SNAFU_to_number(x: str) -> int:
A = {'=': -2, '-': -1, '0': 0, '1': 1, '2': 2, }
return A[x]
def number_to_SNAFU(x: int):
B = {-2: '=', -1: '-', 0: '0', 1: '1', 2: '2', }
return B[x]
def addSNAFU(a: str, b: str) -> str:
z = zip_longest(map(SNAFU_to_number, reversed(a)),
map(SNAFU_to_number, reversed(b)), fillvalue=0)
result = []
carry = 0
for a, b in z:
c = a + b + carry
carry, c = divmod(c+2,5)
result.append(c-2)
if carry != 0:
result.append(carry)
return ''.join(list(map(number_to_SNAFU, reversed(result))))
print(deque(accumulate(data, func=addSNAFU, initial=""), 1).pop())