Advertisement
Guest User

Untitled

a guest
Dec 18th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. # Input: list of available patterns (separated by commas)
  2. #        then blank line, then list of desired designs (one per line)
  3. # Output: how many ways can designs be made by concatenating copies of
  4. #         one or more patterns?
  5.  
  6. from functools import cache
  7.  
  8. @cache
  9. def ways_possible(design):
  10.   ways = 0
  11.   for pattern in patterns:
  12.     if design == pattern:
  13.       ways += 1
  14.     if design[:len(pattern)] == pattern:
  15.       ways += ways_possible(design[len(pattern):])
  16.   return ways
  17.  
  18. patterns = []
  19. designs = []
  20.  
  21. found_blank_line = False
  22. file = open("19_input.txt", "r")
  23. for line in file:
  24.   line = line.replace("\n", "")
  25.   if line == "":
  26.     found_blank_line = True
  27.     continue
  28.   if not found_blank_line:
  29.     for pattern in line.split(", "):
  30.       patterns.append(pattern)
  31.     continue
  32.   designs.append(line)
  33.  
  34. total = 0
  35. for design in designs:
  36.   total += ways_possible(design)
  37. print (total)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement