From 589696266b647df7bfc0c9e982fba8f24a5240f2 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Thu, 12 Dec 2024 23:14:44 +0100 Subject: [PATCH] Day 11 --- day11.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 day11.py diff --git a/day11.py b/day11.py new file mode 100644 index 0000000..8c9ee81 --- /dev/null +++ b/day11.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import functools + +stones = [] +with open("day11") as f: + l = f.read().split(' ') + stones = list(map(int, l)) + +# 1 +def part1(): + for i in range(25): + s = 0 + while s < len(stones): + match stones[s]: + case 0: + stones[s] = 1 + s += 1 + case num if len(str(num))%2==0: + l = len(str(num)) + stones[s] = int(str(num)[:l//2]) + stones.insert(s+1, int(str(num)[-l//2:])) + s += 2 + case _: + stones[s] = stones[s] * 2024 + s += 1 + + print(len(stones)) + +# 2 +@functools.cache +def memo_me(stone, its): + if its == 0: return 1 + + if stone == 0: + return memo_me(1, its-1) + if len(str(stone))%2==0: + l = len(str(stone)) + return memo_me(int(str(stone)[:l//2]), its-1) + memo_me(int(str(stone)[-l//2:]), its-1) + else: + return memo_me(stone*2024, its-1) + + +def part2(): + num_stones = 0 + for stone in stones: + num_stones += memo_me(stone, 75) + print(num_stones) + +part2() \ No newline at end of file