19_towels.py 445 B

12345678910111213141516
  1. #!/usr/bin/env python3
  2. import sys
  3. from functools import cache
  4. @cache
  5. def possible(design, towels):
  6. if not design:
  7. return 1
  8. return sum(possible(design[len(towel):], towels)
  9. for towel in towels if design.startswith(towel))
  10. towels = tuple(sys.stdin.readline().rstrip().split(', '))
  11. designs = sys.stdin.read().split()
  12. pos = [possible(design, towels) for design in designs]
  13. print(sum(map(bool, pos)))
  14. print(sum(pos))