#!/usr/bin/python3
# -*- coding: utf-8 -*-

""" --- Day 19: Medicine for Rudolph ---

Rudolph the Red-Nosed Reindeer is sick!
His nose isn't shining very brightly, and he needs medicine.

Red-Nosed Reindeer biology isn't similar to regular reindeer biology;
Rudolph is going to need custom-made medicine.
Unfortunately, Red-Nosed Reindeer chemistry isn't similar
to regular reindeer chemistry, either.

The North Pole is equipped with a Red-Nosed Reindeer
nuclear fusion/fission plant, capable of constructing any Red-Nosed Reindeer
molecule you need. It works by starting with some input molecule
and then doing a series of replacements, one per step,
until it has the right molecule.

However, the machine has to be calibrated before it can be used.
Calibration involves determining the number of molecules that can be
generated in one step from a given starting point.

For example, imagine a simpler machine that supports
only the following replacements:

H => HO
H => OH
O => HH

Given the replacements above and starting with HOH,
the following molecules could be generated:

HOOH (via H => HO on the first H).
HOHO (via H => HO on the second H).
OHOH (via H => OH on the first H).
HOOH (via H => OH on the second H).
HHHH (via O => HH).

So, in the example above, there are 4 distinct molecules
(not five, because HOOH appears twice) after one replacement from HOH.

Santa's favorite molecule, HOHOHO, can become 7 distinct molecules
(over nine replacements: six from H, and three from O).

The machine replaces without regard for the surrounding characters.
For example, given the string H2O, the transition
H => OO would result in OO2O.

Your puzzle input describes all of the possible replacements and,
at the bottom,the medicine molecule for which you need to calibrate
the machine. How many distinct molecules can be created after all the
different ways you can do one replacement on the medicine molecule? """


# read input
with open('./input.txt', 'r') as f:
    text = f.readlines()


# parsed substitution as tuples of form:
# (target_string, substitution)
substitutions = []

# parse input to get substitutions
# stop at empty line
import re
pattern = re.compile(r'(\w+) => (\w+)')
for line in text:
    if len(line.strip()) == 0:
        break
    target, sub = pattern.match(line).groups()
    substitutions.append((target, sub))

# add string to be changed
string = text[-1]

# store all unique variations in a set
variations = set()

# iterate over each set of substitution
for target, sub in substitutions:
    # set starting position in string at 0
    i = 0
    # continue processing until there are further matches in the string
    # string.find return -1 if there are no further matches
    while True:

        # find target string at or after position i
        i = string.find(target, i)

        # if no string was found, break out of here
        if (i == -1):
            break

        # create the new string as:
        # string[:i] - portion of string before the match
        # sub - string to be substituted
        # string[i + len(target):] - portion of string after match
        new_string = string[:i] + sub + string[i + len(target):]

        # add the new string to the set
        # this will store only unique strings
        variations.add(new_string)

        # increment i to search at the next position
        i += 1


# print the length of unique substitutions possible
print(len(variations))
