from re import sub import sys import os def check_matchp1(p, t): if p=='': return False for ts in t: if p == ts: return True else: if p[:len(ts)] == ts[:len(p)]: if check_matchp1(p[len(ts):], t): return True return False G = {} def match_count(p, t): count = 0 if p in G: return G[p] else: if p=='': return count for ts in t: if p == ts: count += 1 else: if p[:len(ts)] == ts[:len(p)]: sub_count = match_count(p[len(ts):], t) count += sub_count G[p] = count return count def part_one(p, t): p1 = 0 for ps in p: if check_matchp1(ps, t): p1 += 1 print("p1: ", p1) def part_two(p, t): p2 = 0 for ps in p: p2 += match_count(ps, t) print("p2: ", p2) def main(): f = open('input.txt', 'r') contents = [x.strip() for x in f.readlines()] towels = contents[0].split(", ") patterns = contents[2:] part_one(patterns, towels) part_two(patterns, towels) if __name__ == "__main__": main()