19-2
This commit is contained in:
@@ -38,7 +38,6 @@ def check_design(txt: str) -> bool:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
alphabet, designs = read_input(file)
|
alphabet, designs = read_input(file)
|
||||||
alphabet.sort(reverse=True)
|
|
||||||
sol = 0
|
sol = 0
|
||||||
for i, design in enumerate(designs):
|
for i, design in enumerate(designs):
|
||||||
#print(i, design)
|
#print(i, design)
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ def check_design(txt: str) -> bool:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
alphabet, designs = read_input(file)
|
alphabet, designs = read_input(file)
|
||||||
alphabet.sort(reverse=True)
|
|
||||||
sol = 0
|
sol = 0
|
||||||
for i, design in enumerate(designs):
|
for i, design in enumerate(designs):
|
||||||
#print(i, design)
|
#print(i, design)
|
||||||
|
|||||||
49
19/19-2.py
Normal file
49
19/19-2.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
|
file = "./input.txt"
|
||||||
|
#file = "./ex.txt"
|
||||||
|
#file = "./input-e.txt"
|
||||||
|
from time import time
|
||||||
|
start_time = time()
|
||||||
|
|
||||||
|
def read_input(input_file:str, ) -> tuple[list[str], list[str]]:
|
||||||
|
a = []
|
||||||
|
d = []
|
||||||
|
f = open(input_file, "r")
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if line == "":
|
||||||
|
continue
|
||||||
|
elif "," in line:
|
||||||
|
a = line.replace(" ","").split(",")
|
||||||
|
else:
|
||||||
|
d.append(line)
|
||||||
|
f.close()
|
||||||
|
return a, d
|
||||||
|
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def count_designs(txt: str) -> int:
|
||||||
|
global alphabet
|
||||||
|
if txt == "":
|
||||||
|
return 1
|
||||||
|
total = 0
|
||||||
|
for substring in alphabet:
|
||||||
|
if txt.startswith(substring):
|
||||||
|
remaining = txt[len(substring):]
|
||||||
|
total += count_designs(remaining)
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
alphabet, designs = read_input(file)
|
||||||
|
sol = 0
|
||||||
|
for i, design in enumerate(designs):
|
||||||
|
x = count_designs(design)
|
||||||
|
print(i, design, x)
|
||||||
|
sol += x
|
||||||
|
print(sol)
|
||||||
|
|
||||||
|
print(f'Runtime: {time()-start_time:.8f} s')
|
||||||
Reference in New Issue
Block a user