Created
December 11, 2024 18:18
-
-
Save rodrigogiraoserrao/4f218e3cd667abf8152e78d8d0fc184e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# === Parsing === | |
with open("input.txt", "r") as f: | |
nums = map(int, f.read().split()) | |
# === Part 1 === | |
from collections import Counter | |
def stone_blinking(stones, steps): | |
for _ in range(steps): | |
new_stones = Counter() | |
for stone_num, stone_count in stones.items(): | |
stone_str = str(stone_num) | |
if stone_num == 0: | |
new_stones[1] += stone_count | |
elif (l := len(stone_str)) % 2 == 0: | |
left, right = stone_str[: l // 2], stone_str[l // 2 :] | |
new_stones[int(left)] += stone_count | |
new_stones[int(right)] += stone_count | |
else: | |
new_stones[stone_num * 2024] += stone_count | |
stones = new_stones | |
return stones | |
stones = stone_blinking(Counter(nums), 25) | |
print(sum(stones.values())) | |
print(len(stones.keys())) | |
# === Part 2 === | |
import time | |
start = time.perf_counter_ns() | |
stones = stone_blinking(Counter(nums), 75) | |
end = time.perf_counter_ns() | |
print(f"Solved in {(end - start) / pow(10, 9)}s") | |
print(sum(stones.values())) | |
print(len(stones.keys())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment