summaryrefslogtreecommitdiff
path: root/19_2024.py
diff options
context:
space:
mode:
Diffstat (limited to '19_2024.py')
-rw-r--r--19_2024.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/19_2024.py b/19_2024.py
new file mode 100644
index 0000000..4ddf0fc
--- /dev/null
+++ b/19_2024.py
@@ -0,0 +1,29 @@
+infile = "data/19_2024.txt"
+data = open(infile).read().strip()
+words, targets = data.split("\n\n")
+words = words.split(", ")
+p1, p2, dp = 0, 0, {}
+
+
+def ways(words, target):
+ if target in dp:
+ return dp[target]
+ ans = 0
+ if not target:
+ ans = 1
+ for word in words:
+ if target.startswith(word):
+ ans += ways(words, target[len(word) :])
+ dp[target] = ans
+ return ans
+
+
+for target in targets.split("\n"):
+ target_ways = ways(words, target)
+ if target_ways > 0:
+ p1 += 1
+ p2 += target_ways
+
+
+print(p1)
+print(p2)