Skip to content

Instantly share code, notes, and snippets.

@rodrigogiraoserrao
Created December 11, 2024 18:18
Show Gist options
  • Save rodrigogiraoserrao/4f218e3cd667abf8152e78d8d0fc184e to your computer and use it in GitHub Desktop.
Save rodrigogiraoserrao/4f218e3cd667abf8152e78d8d0fc184e to your computer and use it in GitHub Desktop.
# === 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