-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
52 lines (38 loc) · 1.05 KB
/
__init__.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import *
from aocpy import BaseChallenge
SNAFU_DIGITS = {
"2": 2,
"1": 1,
"0": 0,
"-": -1,
"=": -2,
}
REGULAR_DIGITS = {SNAFU_DIGITS[k]: k for k in SNAFU_DIGITS}
def parse(instr: str) -> List[str]:
return instr.strip().splitlines()
def decode_snafu(snafu: str) -> int:
acc = 0
for i, char in enumerate(reversed(snafu)):
acc += SNAFU_DIGITS[char] * pow(5, i)
return acc
def encode_snafu(n: int) -> str:
acc = []
while n != 0:
rem = n % 5
acc.append(rem)
n = n // 5
for i in range(len(acc)):
if acc[i] > 2:
acc[i] = acc[i] - 5
if i + 1 < len(acc):
acc[i + 1] = acc[i + 1] + 1
else:
acc.append(1)
return "".join(map(lambda x: REGULAR_DIGITS[x], reversed(acc)))
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> str:
return encode_snafu(sum(map(decode_snafu, parse(instr))))
@staticmethod
def two(instr: str) -> str:
return "Merry Christmas!"