question: --- Day 19: Linen Layout ---
  
  Today, The Historians take you up to the hot springs on Gear Island! Very suspiciously, absolutely nothing goes wrong as they begin their careful search of the vast field of helixes.
  
  Could this finally be your chance to visit the onsen next door? Only one way to find out.
  
  After a brief conversation with the reception staff at the onsen front desk, you discover that you don't have the right kind of money to pay the admission fee. However, before you can leave, the staff get your attention. Apparently, they've heard about how you helped at the hot springs, and they're willing to make a deal: if you can simply help them arrange their towels, they'll let you in for free!
  
  Every towel at this onsen is marked with a pattern of colored stripes. There are only a few patterns, but for any particular pattern, the staff can get you as many towels with that pattern as you need. Each stripe can be white (w), blue (u), black (b), red (r), or green (g). So, a towel with the pattern ggr would have a green stripe, a green stripe, and then a red stripe, in that order. (You can't reverse a pattern by flipping a towel upside-down, as that would cause the onsen logo to face the wrong way.)
  
  The Official Onsen Branding Expert has produced a list of designs - each a long sequence of stripe colors - that they would like to be able to display. You can use any towels you want, but all of the towels' stripes must exactly match the desired design. So, to display the design rgrgr, you could use two rg towels and then an r towel, an rgr towel and then a gr towel, or even a single massive rgrgr towel (assuming such towel patterns were actually available).
  
  To start, collect together all of the available towel patterns and the list of desired designs (your puzzle input). For example:
  
  r, wr, b, g, bwu, rb, gb, br
  
  brwrr
  bggr
  gbbr
  rrbgbr
  ubwu
  bwurrg
  brgr
  bbrgwb
  The first line indicates the available towel patterns; in this example, the onsen has unlimited towels with a single red stripe (r), unlimited towels with a white stripe and then a red stripe (wr), and so on.
  
  After the blank line, the remaining lines each describe a design the onsen would like to be able to display. In this example, the first design (brwrr) indicates that the onsen would like to be able to display a black stripe, a red stripe, a white stripe, and then two red stripes, in that order.
  
  Not all designs will be possible with the available towels. In the above example, the designs are possible or impossible as follows:
  
  brwrr can be made with a br towel, then a wr towel, and then finally an r towel.
  bggr can be made with a b towel, two g towels, and then an r towel.
  gbbr can be made with a gb towel and then a br towel.
  rrbgbr can be made with r, rb, g, and br.
  ubwu is impossible.
  bwurrg can be made with bwu, r, r, and g.
  brgr can be made with br, g, and r.
  bbrgwb is impossible.
  In this example, 6 of the eight designs are possible with the available towel patterns.
  
  To get into the onsen as soon as possible, consult your list of towel patterns and desired designs carefully. How many designs are possible?
solution: from pprint import pprint
  
  WHITE = "w"
  BLUE = "u"
  BLACK = "b"
  RED = "r"
  GREEN = "g"
  
  INPUT_FILE = "test.txt"
  
  read_file = open(INPUT_FILE, "r")
  
  lines = read_file.readlines()
  
  available_towels = lines[0].strip().split(", ")
  
  designs = []
  
  for line in lines[2:]:
      designs.append(line.strip())
  
  
  def find_first_pattern(design, start_index=0):
      for i in range(len(design), 0, -1):
          if design[start_index:i] in available_towels:
              return (i, design[start_index:i])
      return (None, None)
  
  
  def find_last_pattern(design, end_index):
      for i in range(len(design)):
          if design[i:end_index] in available_towels:
              return (i, design[i:end_index])
  
      return (None, None)
  
  
  def find_largest_from_start(design, start=0):
      largest_pattern_match = None
      end = 0
      for i in range(1 + start, len(design)):
          sub_string = design[start:i]
          print(sub_string)
          if sub_string in available_towels:
              largest_pattern_match = sub_string
              end = i
  
      return (largest_pattern_match, end)
  
  
  results = []
  impossible_designs = []
  
  
  def can_build_word(target, patterns):
      def can_build(remaining, memo=None):
          if memo is None:
              memo = {}
  
          # Base cases
          if not remaining:  # Successfully used all letters
              return True
          if remaining in memo:  # Already tried this combo
              return memo[remaining]
  
          # Try each pattern at the start of our remaining string
          # We can reuse patterns, so no need to track what we've used
          for pattern in patterns:
              if remaining.startswith(pattern):
                  new_remaining = remaining[len(pattern) :]
                  if can_build(new_remaining, memo):
                      memo[remaining] = True
                      print(memo)
                      return True
  
          memo[remaining] = False
          return False
  
      return can_build(target)
  
  
  can_build_count = 0
  
  for design in designs:
      if can_build_word(design, available_towels):
          can_build_count += 1
  
  print(can_build_count)
  
  quit()
  
  for design in designs:
      patterns = {}
      result = 0
      pattern_found = False
      while True:
          (result, pattern) = find_first_pattern(design, result)
          patterns[pattern] = patterns.get(pattern, 0) + 1
          if not result:
              pattern_found = False
              break
          if design[:result] == design:
              pattern_found = True
              break
      if pattern_found:
          results.append((design, patterns))
      else:
          patterns = {}
          result = len(design)
          pattern_found = False
          while True:
              (result, pattern) = find_last_pattern(design, result)
              patterns[pattern] = patterns.get(pattern, 0) + 1
              if result == None:
                  pattern_found = False
                  break
              if result == 0:
                  pattern_found = True
                  break
          if pattern_found:
              results.append((design, patterns))
          else:
              impossible_designs.append(design)
  