This commit is contained in:
Daniel Weber 2024-12-19 11:37:27 -05:00
parent 9abf9acc77
commit 33eadbdbb1

View File

@ -14,33 +14,24 @@ def check_matchp1(p, t):
return True return True
return False return False
G = {}
def match_count(p, t): def match_count(p, t):
count = 0 count = 0
if p=='': if p in G:
return count return G[p]
for ts in t: else:
if p == ts: if p=='':
count += 1 return count
else: for ts in t:
if p[:len(ts)] == ts[:len(p)]: if p == ts:
sub_count = match_count(p[len(ts):], t) count += 1
if sub_count > 0: else:
if p[:len(ts)] == ts[:len(p)]:
sub_count = match_count(p[len(ts):], t)
count += sub_count count += sub_count
G[p] = count
return count return count
def find_substrings(p, t):
ss = []
if p=='':
return ss
for ts in t:
if p == ts:
ss.append(ts)
else:
if p[:len(ts)] == ts[:len(p)]:
ss.append(find_substrings(p[len(ts):], t))
return ss
def part_one(p, t): def part_one(p, t):
p1=0 p1=0
for ps in p: for ps in p:
@ -49,25 +40,16 @@ def part_one(p, t):
print("p1: ", p1) print("p1: ", p1)
def part_two(p, t): def part_two(p, t):
p2=0 p2 = 0
tset = set(t) for ps in p:
for ts in tset: p2 += match_count(ps, t)
print("SUBSTRING: ", ts)
print(find_substrings(ts, tset))
# for c, ps in enumerate(p):
# p2 += match_count(ps, t)
print("p2: ", p2) print("p2: ", p2)
def main(): def main():
f = open('test.txt', 'r') f = open('input.txt', 'r')
contents = [x.strip() for x in f.readlines()] contents = [x.strip() for x in f.readlines()]
print(contents)
towels = contents[0].split(", ") towels = contents[0].split(", ")
patterns = contents[2:] patterns = contents[2:]
print(patterns)
print(towels)
part_one(patterns, towels) part_one(patterns, towels)
part_two(patterns, towels) part_two(patterns, towels)