Subterranean Sustainability


Fork me on GitHub
2018-12-12

Day 12: Subterranean Sustainability

Description:
--- Day 12: Subterranean Sustainability ---

The year 518 is significantly more underground than your history books implied. Either that, or you've arrived in a vast cavern network under the North Pole.

After exploring a little, you discover a long tunnel that contains a row of small pots as far as you can see to your left and right. A few of them contain plants - someone is trying to grow things in these geothermally-heated caves.

The pots are numbered, with 0 in front of you. To the left, the pots are numbered -1, -2, -3, and so on; to the right, 1, 2, 3.... Your puzzle input contains a list of pots from 0 to the right and whether they do (#) or do not (.) currently contain a plant, the initial state. (No other pots currently contain plants.) For example, an initial state of #..##.... indicates that pots 0, 3, and 4 currently contain plants.

Your puzzle input also contains some notes you find on a nearby table: someone has been trying to figure out how these plants spread to nearby pots. Based on the notes, for each generation of plants, a given pot has or does not have a plant based on whether that pot (and the two pots on either side of it) had a plant in the last generation. These are written as LLCRR => N, where L are pots to the left, C is the current pot being considered, R are the pots to the right, and N is whether the current pot will have a plant in the next generation. For example:

A note like ..#.. => . means that a pot that contains a plant but with no plants within two pots of it will not have a plant in it during the next generation.
A note like ##.## => . means that an empty pot with two plants on each side of it will remain empty in the next generation.
A note like .##.# => # means that a pot has a plant in a given generation if, in the previous generation, there were plants in that pot, the one immediately to the left, and the one two pots to the right, but not in the ones immediately to the right and two to the left.

It's not clear what these plants are for, but you're sure it's important, so you'd like to make sure the current configuration of plants is sustainable by determining what will happen after 20 generations.

For example, given the following input:

initial state: #..#.#..##......###...###

...## => #
..#.. => #
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #

For brevity, in this example, only the combinations which do produce a plant are listed. (Your input includes all possible combinations.) Then, the next 20 generations will look like this:

1 2 3
0 0 0 0
0: ...#..#.#..##......###...###...........
1: ...#...#....#.....#..#..#..#...........
2: ...##..##...##....#..#..#..##..........
3: ..#.#...#..#.#....#..#..#...#..........
4: ...#.#..#...#.#...#..#..##..##.........
5: ....#...##...#.#..#..#...#...#.........
6: ....##.#.#....#...#..##..##..##........
7: ...#..###.#...##..#...#...#...#........
8: ...#....##.#.#.#..##..##..##..##.......
9: ...##..#..#####....#...#...#...#.......
10: ..#.#..#...#.##....##..##..##..##......
11: ...#...##...#.#...#.#...#...#...#......
12: ...##.#.#....#.#...#.#..##..##..##.....
13: ..#..###.#....#.#...#....#...#...#.....
14: ..#....##.#....#.#..##...##..##..##....
15: ..##..#..#.#....#....#..#.#...#...#....
16: .#.#..#...#.#...##...#...#.#..##..##...
17: ..#...##...#.#.#.#...##...#....#...#...
18: ..##.#.#....#####.#.#.#...##...##..##..
19: .#..###.#..#.#.#######.#.#.#..#.#...#..
20: .#....##....#####...#######....#.#..##.

The generation is shown along the left, where 0 is the initial state. The pot numbers are shown along the top, where 0 labels the center pot, negative-numbered pots extend to the left, and positive pots extend toward the right. Remember, the initial state begins at pot 0, which is not the leftmost pot used in this example.

After one generation, only seven plants remain. The one in pot 0 matched the rule looking for ..#.., the one in pot 4 matched the rule looking for .#.#., pot 9 matched .##.., and so on.

In this example, after 20 generations, the pots shown as # contain plants, the furthest left of which is pot -2, and the furthest right of which is pot 34. Adding up all the numbers of plant-containing pots after the 20th generation produces 325.

After 20 generations, what is the sum of the numbers of all pots which contain a plant?

--- Part Two ---

You realize that 20 generations aren't enough. After all, these plants will need to last another 1500 years to even reach your timeline, not to mention your future.

After fifty billion (50000000000) generations, what is the sum of the numbers of all pots which contain a plant?

Input:
initial state: .##..#.#..##..##..##...#####.#.....#..#..##.###.#.####......#.......#..###.#.#.##.#.#.###...##.###.#

.##.# => #
##.#. => #
##... => #
#.... => .
.#..# => .
#.##. => .
.##.. => .
.#.## => .
###.. => .
..##. => #
##### => #
#...# => #
.#... => #
###.# => #
#.### => #
##..# => .
.###. => #
...## => .
..#.# => .
##.## => #
....# => .
#.#.# => #
#.#.. => .
.#### => .
...#. => #
..### => .
..#.. => #
..... => .
####. => .
#..## => #
.#.#. => .
#..#. => #

Part 1:
var input_initial = File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"12_input.txt"))[0].Substring(15).Select(p=>p=='#'?1:0).ToArray();
var input_rules = File
	.ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"12_input.txt"))
	.Skip(1)
	.Where(p => !string.IsNullOrWhiteSpace(p))
	.Select( p => ( p.Split(' ')[0].Select(q=>q=='#'?1:0).ToArray() , p.Split(' ')[2][0]=='#'?1:0 ) )
	.Select( p => ( p.Item1.Reverse().Select((u,i)=>(u<<i)).Aggregate((s,t)=>s|t) , p.Item2 ) )
	.OrderBy(p => p.Item1)
	.Select(p => p.Item2)
	.ToArray();

int LEN = 2 * 20 + input_initial.Length + 2 * 20;
int OFF = LEN / 2 - input_initial.Length / 2;

int[] pots = new int[LEN];
int[] ipots = new int[LEN];

//input_initial.Dump();
//input_rules.Dump();

for (int i = 0; i < input_initial.Length; i++) pots[i+OFF]=input_initial[i];

int[] rules = new int[OFF*2+100];
for (int gen = 0; gen < 20; gen++)
{
	//string.Join("", rules.Select(p => p + "")).Dump();
	($"{gen:00}:  "+string.Join("", pots.Select(p => (p == 0) ? "." : "#"))).Dump();

	for (int p = 2; p < pots.Length-2; p++)
	{
		var rule = (pots[p - 2] << 4) | (pots[p - 1] << 3) | (pots[p - 0] << 2) | (pots[p + 1] << 1) | (pots[p + 2] << 0);
		
		rules[p]=rule;
		ipots[p]=input_rules[rule];
	}
	for (int p = 0, q = 0; p < pots.Length; pots[p++] = ipots[q++]) ;
}
($"{20:00}:  " + string.Join("", pots.Select(p => (p == 0) ? "." : "#"))).Dump();

"".Dump();

pots.Select((p,i) => (p,i-OFF)).Where(p => p.p>0).Sum(p=>p.Item2).Dump();
Result: 3738

Part 2:
var input_initial = File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"12_input.txt"))[0].Substring(15).Select(p => p == '#' ? 1 : 0).ToArray();
var input_rules = File
	.ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"12_input.txt"))
	.Skip(1)
	.Where(p => !string.IsNullOrWhiteSpace(p))
	.Select(p => (p.Split(' ')[0].Select(q => q == '#' ? 1 : 0).ToArray(), p.Split(' ')[2][0] == '#' ? 1 : 0))
	.Select(p => (p.Item1.Reverse().Select((u, i) => (u << i)).Aggregate((s, t) => s | t), p.Item2))
	.OrderBy(p => p.Item1)
	.Select(p => p.Item2)
	.ToArray();

int LEN = 2 * 2000 + input_initial.Length + 2 * 2000;
int OFF = LEN / 2 - input_initial.Length / 2;

int[] pots = new int[LEN];
int[] ipots = new int[LEN];

//input_initial.Dump();
//input_rules.Dump();

for (int i = 0; i < input_initial.Length; i++) pots[i + OFF] = input_initial[i];


int hashOffset = 0;
string lastHash = "";

for (long gen = 0; gen < 50000000000L; gen++)
{
	for (int p = 2; p < pots.Length - 2; p++)
	{
		var rule = (pots[p - 2] << 4) | (pots[p - 1] << 3) | (pots[p - 0] << 2) | (pots[p + 1] << 1) | (pots[p + 2] << 0);

		ipots[p] = input_rules[rule];
	}
	for (int p = 0, q = 0; p < pots.Length; pots[p++] = ipots[q++]);

	var currHash = string.Join("", pots.SkipWhile(p => p == 0).Reverse().SkipWhile(p => p == 0).Select(p => p.ToString()));
	if (currHash == lastHash) 
	{
		checked
		{
			var hashOffsetNew = pots.TakeWhile(p => p == 0).Count();
			var drift = (hashOffsetNew - hashOffset);

			var score_curr = pots.Select((p, i) => (p, i - OFF)).Where(p => p.p > 0).Sum(p => p.Item2);
			var count_curr = pots.Sum();
			var score_Future = score_curr + (50_000_000_000 - (gen + 1)) * count_curr;
			score_Future.Dump();

			$"stable after {gen} Generations with generational drift of {drift}".Dump();
			return;
		}
	}
	lastHash = currHash;
	hashOffset = pots.TakeWhile(p => p==0).Count();
}

"ERROR - UNSTABLE".Dump();
Result: 3900000002467


made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript