From 266703cdc06a8971ff6cd2f93ee0f268bf4e5645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Tue, 20 Dec 2022 23:39:26 +0100 Subject: [PATCH] Clean day 20. --- 2022/day20.py | 57 +++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/2022/day20.py b/2022/day20.py index 80f6411..5ff6a5b 100644 --- a/2022/day20.py +++ b/2022/day20.py @@ -4,25 +4,13 @@ import sys class Number: - index: int current: int value: int - def __init__(self, index: int, value: int): - self.index = index - self.current = index + def __init__(self, value: int): + self.current = 0 self.value = value - def __eq__(self, other): - if isinstance(other, Number): - return self.value == other.value - elif isinstance(other, int): - return self.value == other - return False - - def __hash__(self): - return hash(self.index) - def __str__(self): return str(self.value) @@ -35,8 +23,8 @@ def decrypt(numbers: list[Number], key: int, rounds: int) -> int: numbers = numbers.copy() original = numbers.copy() - for number in numbers: - number.current = number.index + for index, number in enumerate(numbers): + number.current = index for _ in range(rounds): for number in original: @@ -46,32 +34,39 @@ def decrypt(numbers: list[Number], key: int, rounds: int) -> int: # need to wrap if target >= len(numbers): - for number_2 in numbers[:index]: - number_2.current += 1 - numbers = [number] + numbers[:index] + numbers[index + 1 :] - target = offset - (len(numbers) - index) + 1 - index = 0 - for number_2 in numbers[index : target + 1]: - number_2.current -= 1 + for number_2 in numbers[target:index]: + number_2.current += 1 + numbers = ( + numbers[:target] + + [number] + + numbers[target:index] + + numbers[index + 1 :] + ) + else: + for number_2 in numbers[index : target + 1]: + number_2.current -= 1 + + numbers = ( + numbers[:index] + + numbers[index + 1 : target + 1] + + [number] + + numbers[target + 1 :] + ) number.current = target - numbers = ( - numbers[:index] - + numbers[index + 1 : target + 1] - + [number] - + numbers[target + 1 :] - ) - index_of_0 = numbers.index(0) # type: ignore + index_of_0 = next( + filter(lambda index: numbers[index].value == 0, range(len(numbers))) + ) return sum( numbers[(index_of_0 + offset) % len(numbers)].value * key for offset in (1000, 2000, 3000) ) -numbers = [Number(i, int(x)) for i, x in enumerate(sys.stdin.readlines())] +numbers = [Number(int(x)) for i, x in enumerate(sys.stdin.readlines())] answer_1 = decrypt(numbers, 1, 1) print(f"answer 1 is {answer_1}")