-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
68 lines (51 loc) · 1.54 KB
/
day11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Advent of Code 2024, Day 11: Plutonian Pebbles.
See: https://adventofcode.com/2024/day/11
"""
import sys
from typing import TextIO
from collections import Counter
def blink(stones: Counter):
next_stones = Counter()
for stone, count in stones.items():
if stone == 0:
next_stones[1] += count
continue
stone_str = str(stone)
if len(stone_str) % 2 == 0:
left, right = (
int(stone_str[: len(stone_str) // 2]),
int(stone_str[len(stone_str) // 2 :]),
)
next_stones[left] += count
next_stones[right] += count
continue
next_stones[stone * 2024] += count
return next_stones
def part_one(file: TextIO) -> int:
"""
Count the amount of stones after blinking 25 times.
"""
stones = Counter(map(int, file.read().split()))
for _ in range(25):
stones = blink(stones)
return sum(stones.values())
def part_two(file: TextIO) -> int:
"""
Count the amount of stones after blinking 75 times.
"""
stones = Counter(map(int, file.read().split()))
for _ in range(75):
stones = blink(stones)
return sum(stones.values())
def main():
"""
The main entrypoint for the script.
"""
filename = sys.argv[0].replace(".py", ".txt")
with open(filename, encoding="utf-8") as file:
print("Part one:", part_one(file))
with open(filename, encoding="utf-8") as file:
print("Part two:", part_two(file))
if __name__ == "__main__":
main()