p1, p2 needs to be optimized so it doesnt run forever

This commit is contained in:
Daniel Weber 2024-12-19 06:43:31 -05:00
parent 3ae819ce26
commit 9abf9acc77

76
2024/day19/main.py Normal file
View File

@ -0,0 +1,76 @@
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
def match_count(p, t):
count = 0
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)
if sub_count > 0:
count += sub_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):
p1=0
for ps in p:
if check_matchp1(ps, t):
p1+=1
print("p1: ", p1)
def part_two(p, t):
p2=0
tset = set(t)
for ts in tset:
print("SUBSTRING: ", ts)
print(find_substrings(ts, tset))
# for c, ps in enumerate(p):
# p2 += match_count(ps, t)
print("p2: ", p2)
def main():
f = open('test.txt', 'r')
contents = [x.strip() for x in f.readlines()]
print(contents)
towels = contents[0].split(", ")
patterns = contents[2:]
print(patterns)
print(towels)
part_one(patterns, towels)
part_two(patterns, towels)
if __name__ == "__main__":
main()