summaryrefslogtreecommitdiff
path: root/2024/aoc2024-d11.py
diff options
context:
space:
mode:
Diffstat (limited to '2024/aoc2024-d11.py')
-rw-r--r--2024/aoc2024-d11.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/2024/aoc2024-d11.py b/2024/aoc2024-d11.py
new file mode 100644
index 0000000..0d6fea4
--- /dev/null
+++ b/2024/aoc2024-d11.py
@@ -0,0 +1,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);