53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
#!/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:
|
|
# split stone
|
|
middle = len(str(stone)) // 2
|
|
tens = 10**middle
|
|
first = stone // tens
|
|
second = stone % tens
|
|
return [first, second]
|
|
|
|
return [stone * 2024]
|
|
|
|
|
|
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()
|