summaryrefslogtreecommitdiff
path: root/2022/day25.py
blob: b608ca55c7de1d4d31680e7c4dd0aa930219e075 (plain)
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
53
#!/usr/bin/env python3

# import numpy as np
from functools import reduce
from re import findall
from copy import deepcopy
import sys

# filename = "in/day25.ref"
filename = "in/day25.pzl"
data = open(filename).read()
lines = [line for line in data.rstrip('\n').split('\n')]
# print(lines)

res1 = 0
res2 = 0

def from_snafu(s):
    val = 0
    for char in s:
        val *= 5
        if char == '0':
            pass
        elif char == '1':
            val += 1
        elif char == '2':
            val += 2
        elif char == '-':
            val -= 1
        elif char == '=':
            val -= 2
    return val

def to_snafu(n):
    # conv = ['00', '01', '02', '1=', '1-', '10', '11', '12', '2=', '2-']
    conv = ['=', '-', '0', '1', '2']
    if -2 <= n <= 2:
        return conv[n+2]
    else:
        if -2 <= n%5 <= 2:
            return to_snafu(n//5) + conv[n%5 - 3]
        elif 2 < n%5:
            return to_snafu(n//5 + 1) + conv[n%5 - 3]

res1_dec = 0
for line in lines:
    res1_dec += from_snafu(line)

res1 = to_snafu(res1_dec)

print('res1:', res1)
print('res2:', res2)