From d6b6bb979ecd8019b3f7dd28adf737f2564423ad Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Thu, 19 Dec 2024 05:49:57 +0000 Subject: [PATCH] day 19 part 2 --- day19/sol2.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 day19/sol2.py diff --git a/day19/sol2.py b/day19/sol2.py new file mode 100644 index 0000000..738bcc9 --- /dev/null +++ b/day19/sol2.py @@ -0,0 +1,31 @@ +from functools import cache +def solve(file): + designs = file.readline().strip().split(", ") + assert file.readline() == '\n' + + ds = set(designs) + + @cache + def ways(x): + n = 0 + if x in ds: + n += 1 + if len(x) > 1: + for i in range(1,len(x)): + if x[:i] in ds: + n += ways(x[i:]) + #print("ways(%s) = %s"%(x,n)) + return n + + t = 0 + for line in file: + towel = line.strip() + w = ways(towel) + #print(w, towel) + t += w + + print(t) + + +solve(open('sample1.in')) +solve(open('input'))