-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.py
52 lines (36 loc) · 1017 Bytes
/
day25.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 functools import cache
data = open("input.txt", "r").read().strip().split("\n")
mapping = {"2": 2, "1": 1, "0": 0, "-": -1, "=": -2}
@cache
def pow5(n: int) -> int:
return pow(5, n)
def parse_snafu(num: str) -> int:
res = 0
for index, char in enumerate(reversed(num)):
digit = mapping[char] * pow5(index)
res += digit
return res
def make_snafu(num: int) -> str:
curr = num
res = ""
while curr:
digit = curr % 5
if digit == 2:
res = "2" + res
elif digit == 1:
res = "1" + res
elif digit == 0:
res = "0" + res
elif digit == 4:
res = "-" + res
curr += 1
elif digit == 3:
res = "=" + res
curr += 2
curr = curr // 5
return res
def part_one(data: list[str]) -> str:
num_to_find = sum([parse_snafu(line) for line in data])
return make_snafu(num_to_find)
if __name__ == "__main__":
print(part_one(data))