-= Puzzle 11: Humongous Trees =-

You decide to get to work early in the morning, because you know that this is going to be a long day. Your eleventh day at BitFlop Laboratories is dedicated to the study of trees.

The botany department has used their knowledge of the Magic Flowerstalk to encode tree growth patterns into DNA.

The trees are expected to grow into Humongous Trees, and the botany department has asked you to help them figure out how they will grow.

The problem is, it takes years for a tree to grow, and the botany department doesn't have that kind of time. So they have asked you to simulate the growth of the trees.

The trees are represented as a DNA configuration, which is a configuration of numbers that represent what each segment of the tree grows into.

The trees grow on a grid, and it is made of parts. Each part is either a sprout or a stem.

-= DNA =-

They show you an example of a very simple DNA configuration:

    02          XX          00
01  00  XX  01  01  02  XX  02  XX

This represents what each segment of the tree grows into.

Each DNA id describes three possible sprouts: one above, one to the left, and one to the right.

For example, the segment of the DNA configuration that is defined for 00 is:

    02
01  00  XX

This means that any sprout of id 00 will grow a sprout of id 01 on the left, a sprout of id 02 above it, and no sprout to the right (XX means no sprout).

A tree starts out as a single sprout of id 00, which for the purpose of the visualization is represented as a @ character. The sprout grows into a stem, which is represented as a # character.

But this happens after the sprout has a chance to grow its own sprouts according to the DNA configuration. (even if it fails to grow any sprouts, it will still turn into a stem)

Every year the tree grows its sprouts simultaneously.

For the example above, the tree starts out like:

01:.@.

But after 1 year it grows into:

02:..@.
01:.@#.

The original sprout turns into a stem, after it also grows a new sprout above it (of id 02) and a new sprout to the left of it (of id 01).

This is defined in the DNA configuration.

After 2 years, the tree grows into:

03:...@.
02:...#.
01:.@##.

After 3 years:

04:....@.
03:...@#.
02:....#.
01:.@###.

After 4 years:

05:.....@.
04:.....#.
03:...@##.
02:.....#.
01:.@####.

After 5 years:

06:......@.
05:.....@#.
04:......#.
03:...@###.
02:......#.
01:.@#####.

And so on...

Important: What happens if 2 sprouts of the same tree grow into the same location? The resulting sprout with the highest DNA id will take priority and grow into that location (not the sprouts it came from, it's about the sprout ids that try to appear there).

-= Energy and Death =-

You might think that the tree will grow forever, but it won't. The tree will eventually die.

There are two ways a tree can die:

Every time after sprouts appear, the tree harnesses energy from the sun. Each segment of the tree (both sprouts and stems) require 3 energy to live.

After the growth stage of the tree, you check if the tree has enough energy to support all of its parts. If it does, it will continue to grow. If it doesn't, it will die and stop growing.

Don't fret! The trees use photosynthesis to harness energy from the sun, and they harness it in the following way:

Important: These energy mechanics only go into affect when the tree turns 5 years old. Before that, the tree will always have enough energy to survive (from the soil).

For the example tree from above, after 5 years, the tree has a total of 15 parts (11 stems and 4 sprouts). so it requires 15 * 3 = 45 energy to survive.

Looking at where it gets the energy from:

06:......@.
05:.....@#. // height 5, produces 15 energy
04:......#. // 8 energy (1 stem above it)
03:...@###. // 9 energy, 9 energy and 3 energy
02:......#. // no energy (3 stems above it)
01:.@#####. // 3 + 3 + 2 + 2 + 0 energy

This means the total energy the tree gets here is 54, which is more than enough to support the 45 energy it needs to survive. So it will continue to grow.

And grow it does! But after 67 years, unfortunately, the tree dies because it runs out of energy.

The tree then look like this:

68:....................................................................@.
67:...................................................................@#.
66:....................................................................#.
65:.................................................................@###.
64:....................................................................#.
63:...............................................................@#####.
62:....................................................................#.
61:.............................................................@#######.
60:....................................................................#.
59:...........................................................@#########.
58:....................................................................#.
57:.........................................................@###########.
56:....................................................................#.
55:.......................................................@#############.
54:....................................................................#.
53:.....................................................@###############.
52:....................................................................#.
51:...................................................@#################.
50:....................................................................#.
49:.................................................@###################.
48:....................................................................#.
47:...............................................@#####################.
46:....................................................................#.
45:.............................................@#######################.
44:....................................................................#.
43:...........................................@#########################.
42:....................................................................#.
41:.........................................@###########################.
40:....................................................................#.
39:.......................................@#############################.
38:....................................................................#.
37:.....................................@###############################.
36:....................................................................#.
35:...................................@#################################.
34:....................................................................#.
33:.................................@###################################.
32:....................................................................#.
31:...............................@#####################################.
30:....................................................................#.
29:.............................@#######################################.
28:....................................................................#.
27:...........................@#########################################.
26:....................................................................#.
25:.........................@###########################################.
24:....................................................................#.
23:.......................@#############################################.
22:....................................................................#.
21:.....................@###############################################.
20:....................................................................#.
19:...................@#################################################.
18:....................................................................#.
17:.................@###################################################.
16:....................................................................#.
15:...............@#####################################################.
14:....................................................................#.
13:.............@#######################################################.
12:....................................................................#.
11:...........@#########################################################.
10:....................................................................#.
09:.........@###########################################################.
08:....................................................................#.
07:.......@#############################################################.
06:....................................................................#.
05:.....@###############################################################.
04:....................................................................#.
03:...@#################################################################.
02:....................................................................#.
01:.@###################################################################.

The tree here harnesses a total of 3640 energy, but it requires 3672 energy to survive, so it dies...

Your task is to determine the biological mass of the tree after it has died. The biological mass is defined as the total number of segments (both sprouts and stems) that the tree has when it dies.

For the example tree, when it dies after year 67, it has a total of 1224 segments (1189 stems, 35 sprouts), so it has a biological mass of 1224.

Your puzzle input contains more then one DNA configuration, and you need to determine the biological mass of each tree after it perishes, and then sum all of the biological masses together to get the final answer.

What is the total biological mass of all the trees after they all die?

You must login to get your input and play!