summaryrefslogtreecommitdiff
path: root/2024/day11
diff options
context:
space:
mode:
authorBotond Hende <nettingman@gmail.com>2024-12-11 23:18:33 +0100
committerBotond Hende <nettingman@gmail.com>2024-12-11 23:18:33 +0100
commit1d3fff4219458283ce7919c3fb5c94951a66dac7 (patch)
tree2c15952a4025fba909706712ea11f332f37d8b1e /2024/day11
parent3b1b3fe99e6953b53a44c070d227ad36341e71c4 (diff)
2024 day11
Diffstat (limited to '2024/day11')
-rw-r--r--2024/day11/input1
-rw-r--r--2024/day11/input_test1
-rw-r--r--2024/day11/solve1_2.py50
3 files changed, 52 insertions, 0 deletions
diff --git a/2024/day11/input b/2024/day11/input
new file mode 100644
index 0000000..6142429
--- /dev/null
+++ b/2024/day11/input
@@ -0,0 +1 @@
+4 4841539 66 5279 49207 134 609568 0 \ No newline at end of file
diff --git a/2024/day11/input_test b/2024/day11/input_test
new file mode 100644
index 0000000..528f9d5
--- /dev/null
+++ b/2024/day11/input_test
@@ -0,0 +1 @@
+125 17 \ No newline at end of file
diff --git a/2024/day11/solve1_2.py b/2024/day11/solve1_2.py
new file mode 100644
index 0000000..e2eef43
--- /dev/null
+++ b/2024/day11/solve1_2.py
@@ -0,0 +1,50 @@
+memoize = {}
+class Globals:
+ memoize_used = 0
+
+def count_digits(number: int) -> int:
+ if number == 0:
+ return 1
+
+ count = 0
+ while number != 0:
+ number = number // 10
+ count += 1
+
+ return count
+
+def count_after_blinks(stone: int, blink_remaining: int):
+ if blink_remaining == 0:
+ return 1
+
+ elif (stone, blink_remaining) in memoize:
+ Globals.memoize_used += 1
+ return memoize[(stone, blink_remaining)]
+
+ else:
+ if stone == 0:
+ result = count_after_blinks(1, blink_remaining - 1)
+
+ else:
+ digits = count_digits(stone)
+ if digits % 2 == 0:
+ divisor = 10 ** (digits // 2)
+ result = count_after_blinks(stone % divisor, blink_remaining - 1) + count_after_blinks(stone // divisor, blink_remaining - 1)
+
+ else:
+ result = count_after_blinks(stone * 2024, blink_remaining - 1)
+
+ memoize[(stone, blink_remaining)] = result
+ return result
+
+data = []
+with open("input") as f:
+ data.extend((int(elem) for elem in f.read().strip().split()))
+
+count = 0
+for elem in data:
+ count += count_after_blinks(elem, 75)
+ print(f"elem {elem} done")
+
+print(Globals.memoize_used)
+print(count) \ No newline at end of file