diff options
| author | nekineki <nekineki@nekineki.net> | 2022-12-25 07:23:55 +0100 |
|---|---|---|
| committer | nekineki <nekineki@nekineki.net> | 2022-12-25 07:23:55 +0100 |
| commit | edc8466e498370dec425379915b83df49de5c88d (patch) | |
| tree | 7428b01632a1fdc03415c6fcee455520d72e71ee /2022/day25.py | |
| parent | 1addc1ee10820384de416d30a0b9be595f7dc648 (diff) | |
day25
Diffstat (limited to '2022/day25.py')
| -rwxr-xr-x | 2022/day25.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/2022/day25.py b/2022/day25.py new file mode 100755 index 0000000..b608ca5 --- /dev/null +++ b/2022/day25.py @@ -0,0 +1,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) + |
