AoC day 19 p2

 avatar
PantsB
python
7 months ago
1.1 kB
44
No Index
from functools import lru_cache
import time

filepath = '<redacted>'
advent1file=open(filepath,'r')
start_time = time.time()
listoflines =advent1file.readlines()
towels=[x.strip() for  x in listoflines[0].strip().split(",")]

def checkit(patt,towels):
    if len(patt)==0:
        return True
    for tow in towels:
        if patt.startswith(tow):
            if checkit(patt[len(tow):],towels):
                return True
    return False

@lru_cache
def checkitsmart(patt):
    global towels
    retval=0
    if len(patt)==0:
        return 1
    for siz in towels.keys():
        if len(patt)<siz:
            continue
        smallpatt=patt[:siz]
        if smallpatt in towels[siz]:
            retval+=checkitsmart(patt[siz:])
    return retval

cando=0

smarttowels={}
for tow in towels:
    if len(tow) not in smarttowels.keys():
        smarttowels[len(tow)]=set()
    smarttowels[len(tow)].add(tow)


towels=smarttowels

for i in range(2,len(listoflines)):
    cando+=checkitsmart(listoflines[i].strip())
        

print(cando)
tim=time.time()-start_time
print(tim)
Editor is loading...
Leave a Comment