1
0
Fork 0
adventofcode/2024/11/main.py

54 lines
1.3 KiB
Python
Raw Normal View History

2024-12-11 09:33:54 +00:00
#!/usr/bin/env python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# Author: Sebastian Mark
# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de)
# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string
def readinput():
with open("input", "r", encoding="utf-8") as file:
# initialize with a count of 1 for each number
stones = {int(n): 1 for n in file.readline().split()}
return stones
def blink(stone: str) -> list:
if stone == 0:
return [1]
if len(str(stone)) % 2 == 0:
2024-12-11 09:33:54 +00:00
# split stone
2024-12-11 09:33:54 +00:00
middle = len(str(stone)) // 2
2024-12-11 09:33:54 +00:00
tens = 10**middle
first = stone // tens
second = stone % tens
return [first, second]
2024-12-11 09:33:54 +00:00
2024-12-11 09:33:54 +00:00
return [stone * 2024]
2024-12-11 09:33:54 +00:00
def solve(stones: dict) -> dict:
new_stones = {}
for stone, count in stones.items():
for num in blink(stone):
# count of each number, remember existing occurences
new_stones[num] = new_stones.get(num, 0) + count
return new_stones
def main():
# part 1 + 2
for r in [25, 75]:
stones = readinput()
for _ in range(r):
stones = solve(stones)
print("Number of stones (%d blinks): %d" % (r, sum(stones.values())))
if __name__ == "__main__":
main()