First part was solved using a list to store stones, since I misinterpreted this paragraph: > No matter how the stones change, their order is > preserved, and they stay on their perfectly > straight line. But since the puzzle question do not require the stones in the correct order, a list (or an OrderedDict) is not required. The pt 1 solution code got killed for eating all RAM, so valuable minutes was lost trying to create a generator instead. It failed. When searching for clues, I ran across multiple people instead utilizing dicts to count occourences. "but what of the order?", I asked, and realized only the counts was relevant.
38 lines
1,000 B
Python
38 lines
1,000 B
Python
from collections import defaultdict
|
|
|
|
from output import ints
|
|
|
|
|
|
def solve(data):
|
|
state = defaultdict(int)
|
|
for e in ints(data):
|
|
state[e] += 1
|
|
for lap in range(75):
|
|
if lap == 25:
|
|
stones_count_25 = sum(state.values())
|
|
queue = list(map(lambda x: x, state.items()))
|
|
for e, delta in queue:
|
|
if e == 0:
|
|
state[1] += delta
|
|
state[0] -= delta
|
|
elif len(str(e)) % 2 == 0:
|
|
i = len(str(e)) // 2
|
|
l, r = int(str(e)[:i]), int(str(e)[i:])
|
|
state[l] += delta
|
|
state[r] += delta
|
|
state[e] -= delta
|
|
else:
|
|
state[e * 2024] += delta
|
|
state[e] -= delta
|
|
stones_count_75 = sum(state.values())
|
|
return stones_count_25, stones_count_75
|
|
|
|
|
|
if __name__ == "__main__":
|
|
with open("./input/11.txt", "r") as f:
|
|
inp = f.read().strip()
|
|
|
|
p1, p2 = solve(inp)
|
|
|
|
print(p1)
|
|
print(p2)
|