add 2024/day11
This commit is contained in:
parent
cac9efe9d3
commit
137880a94d
1 changed files with 51 additions and 0 deletions
51
2024/11/main.py
Normal file
51
2024/11/main.py
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
#!/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:
|
||||||
|
middle = len(str(stone)) // 2
|
||||||
|
first = str(stone)[:middle]
|
||||||
|
second = str(stone)[middle:]
|
||||||
|
return [int(first), int(second)]
|
||||||
|
|
||||||
|
return [int(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()
|
Loading…
Reference in a new issue