diff --git a/11/11-1.py b/11/11-1.py new file mode 100644 index 0000000..e69f704 --- /dev/null +++ b/11/11-1.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +#puzzle_input = "125 17" #example 6x blink=22 Stones / 25x blink = 55312 +puzzle_input = "112 1110 163902 0 7656027 83039 9 74" +x_blink = 25 + +from time import time + +start_time = time() +def str_list_to_int_list(str_list:list[str]) ->list[int]: + result = [] + for part in str_list: + result.append(int(part)) + return result + +def split_integer(num): + length = len(str(num)) + divisor = 10 ** (length // 2) + left = num // divisor + right = num % divisor + return left, right + + +def blink(int_list:list[int]) -> list[int]: + result = [] + for part in int_list: + if part == 0: + result.append(1) + elif len(str(part)) % 2 == 0: + result.extend(split_integer(part)) + else: + result.append(part * 2024) + return result + + +if __name__ == "__main__": + puzzle = puzzle_input.split() + puzzle = str_list_to_int_list(puzzle) + for x in range(x_blink): + puzzle = blink(puzzle) + #print(puzzle) + print(f'input: {puzzle_input} and {x_blink} blinks -> {len(puzzle)} stones') + print(f'Runtime: {time()-start_time:.2f} s') \ No newline at end of file diff --git a/11/11-2-fail.py b/11/11-2-fail.py new file mode 100644 index 0000000..8ab2b58 --- /dev/null +++ b/11/11-2-fail.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +print("Does not work with only 32 GB of RAM......") + + +#puzzle_input = "125 17" #example 6x blink=22 Stones / 25x blink = 55312 +puzzle_input = "112 1110 163902 0 7656027 83039 9 74" +x_blink = 75 + +MULTIPLY_FACTOR = 2024 + +from time import time +import numpy as np +from numba import njit + +@njit() +def blink(int_array:np.ndarray) -> np.ndarray: + result = [] + for part in int_array: + if part == 0: + result.append(1) + elif len(str(part)) % 2 == 0: + left, right = divmod(part, 10**(len(str(part))//2)) + result.extend([left, right]) + else: + result.append(part * MULTIPLY_FACTOR) + return np.array(result, dtype=np.int64) + +@njit() +def do_x_blinks(x:int, arr:np.ndarray) -> int: + for _ in range(x): + arr = blink(arr) + return len(arr) + + +if __name__ == "__main__": + start_time = time() + puzzle = np.array([int(x) for x in puzzle_input.split()], dtype=np.int64) + p_len = do_x_blinks(x_blink, puzzle) + #print(puzzle) + print(f'input: {puzzle_input} and {x_blink} blinks -> {p_len} stones') + print(f'Runtime: {time()-start_time:.2f} s') \ No newline at end of file