Guest User

Untitled

a guest
Dec 18th, 2024
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | Source Code | 0 0
  1. from functools import cache
  2.  
  3. # D Y N A M I C  P R O G R A M M I N G
  4.  
  5. path = "day_19.txt"
  6. # path = "test.txt"
  7.  
  8. with open(path) as f:
  9.     input1, input2 = f.read().split('\n\n')
  10.  
  11.     patterns = frozenset([t.strip() for t in input1.split(',')])
  12.     towels = [t.strip() for t in input2.splitlines()]
  13.    
  14. # print(patterns, towels)
  15.  
  16. @cache
  17. def count_possibilities(patterns, towel: str):
  18.     if len(towel) == 0:
  19.         return 1
  20.  
  21.     count = 0
  22.     for p in patterns:
  23.         if towel.startswith(p):
  24.             sub_towel = towel[len(p):]
  25.             count += count_possibilities(patterns, sub_towel)
  26.            
  27.    
  28.     return count
  29.  
  30. p1 = 0
  31. p2 = 0
  32. for t in towels:
  33.     total = count_possibilities(patterns, t)
  34.     p1 += 1 if total > 0 else 0
  35.     p2 += total
  36.    
  37. print('p1: ', p1)
  38. print('p2: ', p2)
Advertisement
Add Comment
Please, Sign In to add comment