From 4b571136ba54f5df0c1a91c755fbc13fa8f6bc12 Mon Sep 17 00:00:00 2001 From: Lachlan Meyer Date: Thu, 19 Dec 2024 19:12:51 +1100 Subject: [PATCH] 19-2 --- 2024/19/19-2.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2024/19/19-2.py diff --git a/2024/19/19-2.py b/2024/19/19-2.py new file mode 100644 index 0000000..38d71d7 --- /dev/null +++ b/2024/19/19-2.py @@ -0,0 +1,34 @@ +import functools + +patterns = set() +combinations = set() + +@functools.cache +def testComboIsValid(combo): + if len(combo) == 0: + return 1 + + goodCombosFound = 0 + for pattern in patterns: + if combo.startswith(pattern): + remainingCombo = combo.replace(pattern, '', 1) + goodCombosFound += testComboIsValid(remainingCombo) + return goodCombosFound + +with open('2024/19/input.txt') as f: + for i, line in enumerate(f): + line = line.strip() + + if i == 0: + patterns = set(line.split(', ')) + elif i == 1: + continue + else: + combinations.add(line) + + +counter = 0 +for combo in combinations: + counter += testComboIsValid(combo) + +print(counter) \ No newline at end of file