Ver Fonte

Solve day 11

Taddeus Kroes há 1 ano atrás
pai
commit
da398d67aa
2 ficheiros alterados com 31 adições e 0 exclusões
  1. 30 0
      2024/11_pebbles.py
  2. 1 0
      2024/input/11

+ 30 - 0
2024/11_pebbles.py

@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+import sys
+from collections import Counter
+
+def change(stone):
+    if stone == 0:
+        yield 1
+    else:
+        s = str(stone)
+        l = len(s)
+        if l % 2 == 0:
+            yield int(s[:l // 2])
+            yield int(s[l // 2:])
+        else:
+            yield stone * 2024
+
+def blink(counts, times):
+    for _ in range(times):
+        new = Counter()
+        for stone, occurrences in counts.items():
+            for newstone in change(stone):
+                new[newstone] += occurrences
+        counts = new
+    return counts
+
+counts = Counter(map(int, next(sys.stdin).split()))
+counts = blink(counts, 25)
+print(sum(counts.values()))
+counts = blink(counts, 50)
+print(sum(counts.values()))

+ 1 - 0
2024/input/11

@@ -0,0 +1 @@
+1750884 193 866395 7 1158 31 35216 0