blob: 0d6fea44f6e26dee858d3d5547c20d79c998dabd (
plain)
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
|
#advent of code 2024
#day 11
#I liked it. what I did was:
#store current stones in dictionary in format stonenumber:amount of these stones
#iterate the changes in time
#if the given stone is split up for the first time, the result is memorized
#in the changes dictionary
#during iteration, the UpdateStones function tries to read the results of splitting
#if there are none, the stone is getting split up for the first time
#repeat for given steps
stones = {};
changes = {};
f = open("11.in","r");
for s in f.readline().strip().split(" "):
si = int(s);
if si not in stones: stones[si] = 0;
stones[si] += 1;
f.close();
def SplitStones(stone):
NewStones = [];
if stone == 0:
NewStones.append(1);
elif len(str(stone))%2==0:
stonestring = str(stone);
sm = len(stonestring)//2;
s1,s2 = stonestring[0:sm], stonestring[sm:];
NewStones.append(int(s1));
NewStones.append(int(s2));
else:
NewStones.append(stone*2024);
return NewStones;
def UpdateStones(prevStones):
Updated = {};
for s in prevStones:
if s not in changes: changes[s] = SplitStones(s);
for c in changes[s]:
if c not in Updated: Updated[c] = 0;
Updated[c] += prevStones[s];
return Updated;
steps1 = 25; #for part 1
steps2 = 75; #for part 2
for step in range(1,steps2+1):
stones = UpdateStones(stones);
if step == steps1: part1 = sum([stones[x] for x in stones]);
part2 = sum([stones[x] for x in stones]);
print("part 1 =", part1);
print("part 2 =", part2);
|