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

View File

@ -14,8 +14,12 @@ 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 in G:
return G[p]
else:
if p=='': if p=='':
return count return count
for ts in t: for ts in t:
@ -24,23 +28,10 @@ def match_count(p, t):
else: else:
if p[:len(ts)] == ts[:len(p)]: if p[:len(ts)] == ts[:len(p)]:
sub_count = match_count(p[len(ts):], t) sub_count = match_count(p[len(ts):], t)
if sub_count > 0:
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:
@ -50,24 +41,15 @@ def part_one(p, t):
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)