Files
AdventOfCode2024/11/11-2-ki.py
2024-12-11 13:04:50 +01:00

44 lines
1.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from time import time
from collections import Counter
# Puzzle-Input
input_stones = [112, 1110, 163902, 0, 7656027, 83039, 9, 74]
def transform_stone(stone):
if stone == 0:
return [1]
elif len(str(stone)) % 2 == 0:
mid = len(str(stone)) // 2
left = int(str(stone)[:mid])
right = int(str(stone)[mid:])
return [left, right]
else:
return [stone * 2024]
def blink(stones, times):
stone_counts = Counter(stones)
for i in range(1, times + 1):
new_stone_counts = Counter()
for stone, count in stone_counts.items():
for new_stone in transform_stone(stone):
new_stone_counts[new_stone] += count
stone_counts = new_stone_counts
total_stones = sum(stone_counts.values())
print(f"Blinzeln {i}: {total_stones} Steine")
return total_stones
start = time()
# Teil 1: 25 Mal blinzeln
result_part1 = blink(input_stones, 25)
print(f"Nach 25 Mal Blinzeln: {result_part1} Steine")
# Teil 2: 75 Mal blinzeln
result_part2 = blink(input_stones, 75)
print(f"Nach 75 Mal Blinzeln: {result_part2} Steine")
print(f'\nLaufzeit: {time()-start:.2f} s')