Planet of Discord


Fork me on GitHub
2019-12-24

Day 24: Planet of Discord

Description:
--- Day 24: Planet of Discord ---

You land on Eris, your last stop before reaching Santa. As soon as you do, your sensors start picking up strange life forms moving around: Eris is infested with bugs! With an over 24-hour roundtrip for messages between you and Earth, you'll have to deal with this problem on your own.

Eris isn't a very large place; a scan of the entire area fits into a 5x5 grid (your puzzle input). The scan shows bugs (#) and empty spaces (.).

Each minute, The bugs live and die based on the number of bugs in the four adjacent tiles:

A bug dies (becoming an empty space) unless there is exactly one bug adjacent to it.
An empty space becomes infested with a bug if exactly one or two bugs are adjacent to it.

Otherwise, a bug or empty space remains the same. (Tiles on the edges of the grid have fewer than four adjacent tiles; the missing tiles count as empty space.) This process happens in every location simultaneously; that is, within the same minute, the number of adjacent bugs is counted for every tile first, and then the tiles are updated.

Here are the first few minutes of an example scenario:

Initial state:
....#
#..#.
#..##
..#..
#....

After 1 minute:
#..#.
####.
###.#
##.##
.##..

After 2 minutes:
#####
....#
....#
...#.
#.###

After 3 minutes:
#....
####.
...##
#.##.
.##.#

After 4 minutes:
####.
....#
##..#
.....
##...

To understand the nature of the bugs, watch for the first time a layout of bugs and empty spaces matches any previous layout. In the example above, the first layout to appear twice is:

.....
.....
.....
#....
.#...

To calculate the biodiversity rating for this layout, consider each tile left-to-right in the top row, then left-to-right in the second row, and so on. Each of these tiles is worth biodiversity points equal to increasing powers of two: 1, 2, 4, 8, 16, 32, and so on. Add up the biodiversity points for tiles with bugs; in this example, the 16th tile (32768 points) and 22nd tile (2097152 points) have bugs, a total biodiversity rating of 2129920.

What is the biodiversity rating for the first layout that appears twice?

--- Part Two ---

After careful analysis, one thing is certain: you have no idea where all these bugs are coming from.

Then, you remember: Eris is an old Plutonian settlement! Clearly, the bugs are coming from recursively-folded space.

This 5x5 grid is only one level in an infinite number of recursion levels. The tile in the middle of the grid is actually another 5x5 grid, the grid in your scan is contained as the middle tile of a larger 5x5 grid, and so on. Two levels of grids look like this:

| | | |
| | | |
| | | |
-----+-----+---------+-----+-----
| | | |
| | | |
| | | |
-----+-----+---------+-----+-----
| | | | | | | |
| |-+-+-+-+-| |
| | | | | | | |
| |-+-+-+-+-| |
| | | |?| | | |
| |-+-+-+-+-| |
| | | | | | | |
| |-+-+-+-+-| |
| | | | | | | |
-----+-----+---------+-----+-----
| | | |
| | | |
| | | |
-----+-----+---------+-----+-----
| | | |
| | | |
| | | |

(To save space, some of the tiles are not drawn to scale.) Remember, this is only a small part of the infinitely recursive grid; there is a 5x5 grid that contains this diagram, and a 5x5 grid that contains that one, and so on. Also, the ? in the diagram contains another 5x5 grid, which itself contains another 5x5 grid, and so on.

The scan you took (your puzzle input) shows where the bugs are on a single level of this structure. The middle tile of your scan is empty to accommodate the recursive grids within it. Initially, no other levels contain bugs.

Tiles still count as adjacent if they are directly up, down, left, or right of a given tile. Some tiles have adjacent tiles at a recursion level above or below its own level. For example:

| | | |
1 | 2 | 3 | 4 | 5
| | | |
-----+-----+---------+-----+-----
| | | |
6 | 7 | 8 | 9 | 10
| | | |
-----+-----+---------+-----+-----
| |A|B|C|D|E| |
| |-+-+-+-+-| |
| |F|G|H|I|J| |
| |-+-+-+-+-| |
11 | 12 |K|L|?|N|O| 14 | 15
| |-+-+-+-+-| |
| |P|Q|R|S|T| |
| |-+-+-+-+-| |
| |U|V|W|X|Y| |
-----+-----+---------+-----+-----
| | | |
16 | 17 | 18 | 19 | 20
| | | |
-----+-----+---------+-----+-----
| | | |
21 | 22 | 23 | 24 | 25
| | | |

Tile 19 has four adjacent tiles: 14, 18, 20, and 24.
Tile G has four adjacent tiles: B, F, H, and L.
Tile D has four adjacent tiles: 8, C, E, and I.
Tile E has four adjacent tiles: 8, D, 14, and J.
Tile 14 has eight adjacent tiles: 9, E, J, O, T, Y, 15, and 19.
Tile N has eight adjacent tiles: I, O, S, and five tiles within the sub-grid marked ?.

The rules about bugs living and dying are the same as before.

For example, consider the same initial state as above:

....#
#..#.
#.?##
..#..
#....

The center tile is drawn as ? to indicate the next recursive grid. Call this level 0; the grid within this one is level 1, and the grid that contains this one is level -1. Then, after ten minutes, the grid at each level would look like this:

Depth -5:
..#..
.#.#.
..?.#
.#.#.
..#..

Depth -4:
...#.
...##
..?..
...##
...#.

Depth -3:
#.#..
.#...
..?..
.#...
#.#..

Depth -2:
.#.##
....#
..?.#
...##
.###.

Depth -1:
#..##
...##
..?..
...#.
.####

Depth 0:
.#...
.#.##
.#?..
.....
.....

Depth 1:
.##..
#..##
..?.#
##.##
#####

Depth 2:
###..
##.#.
#.?..
.#.##
#.#..

Depth 3:
..###
.....
#.?..
#....
#...#

Depth 4:
.###.
#..#.
#.?..
##.#.
.....

Depth 5:
####.
#..#.
#.?#.
####.
.....

In this example, after 10 minutes, a total of 99 bugs are present.

Starting with your scan, how many bugs are present after 200 minutes?

Input:
#.#..
.#.#.
#...#
.#..#
##.#.

Part 1:
namespace AdventOfCode2019_24_1
{
	const DAY     = 24;
	const PROBLEM = 1;

	export async function run()
	{
		let input = await AdventOfCode.getInput(DAY);
		if (input == null) return;

		AdventOfCode.setIntermedOutputSize("5.00vw");

		let grid = input
			.split(new RegExp('\r?\n'))
			.filter(p => p.trim().length > 0)
			.map(p => p.trim().split('').map(q => q==='#'));

		await AdventOfCode.outputIntermed(tostr(grid));

		let hist: {[_:number]:boolean} = {};
		hist[biodiv(grid)] = true;

		for(;;)
		{
			grid = step(grid);
			let bd = biodiv(grid);
			if (bd in hist)
			{
				AdventOfCode.output(DAY, PROBLEM, bd.toString());
				return;
			}
			hist[bd] = true;

			await AdventOfCode.outputIntermed(tostr(grid));
		}
	}

	function tostr(grid: boolean[][]): string
	{
		return grid.map(p => p.map(q => q ? "#" : ".").join("") ).join("\n");
	}

	function biodiv(grid: boolean[][]): number
	{
		let n = 0;
		for(let y=0; y<5; y++)
		for(let x=0; x<5; x++)
		{
			n += grid[y][x] ? Math.pow(2, y*5+x) : 0;
		}
		return n;
	}

	function step(grid: boolean[][]): boolean[][]
	{
		let g2 = [
			[false, false, false, false, false],
			[false, false, false, false, false],
			[false, false, false, false, false],
			[false, false, false, false, false],
			[false, false, false, false, false]
		];

		for(let y=0; y<5; y++)
		for(let x=0; x<5; x++)
		{
			let adjac = 0;
			if (x>0 && grid[y][x-1]) adjac++;
			if (y>0 && grid[y-1][x]) adjac++;
			if (x<4 && grid[y][x+1]) adjac++;
			if (y<4 && grid[y+1][x]) adjac++;

			if (grid[y][x] && adjac !== 1) g2[y][x] = false; // A bug dies (becoming an empty space) unless there is exactly one bug adjacent to it.
			else if (!grid[y][x] && (adjac === 1 || adjac === 2)) g2[y][x] = true; // An empty space becomes infested with a bug if exactly one or two bugs are adjacent to it.
			else g2[y][x] = grid[y][x]; // Otherwise, a bug or empty space remains the same.
		}
		return g2;
	}
}
Result: 25719471

Part 2:
namespace AdventOfCode2019_24_2
{
	const DAY     = 24;
	const PROBLEM = 2;

	export async function run()
	{
		let input = await AdventOfCode.getInput(DAY);
		if (input == null) return;

		AdventOfCode.setIntermedOutputSize("0.70vw");

		const grid0 = input
			.split(new RegExp('\r?\n'))
			.filter(p => p.trim().length > 0)
			.map(p => p.trim().split('').map(q => q==='#'));

		let world: {[_:number]:boolean} = {};
		for(let y=0; y<5; y++)	for(let x=0; x<5; x++) world[id(x, y, 0)] = grid0[y][x];

		let minD = -1;
		let maxD = +1;

		await AdventOfCode.outputIntermed(tostr2(world, minD, maxD));
		await AdventOfCode.sleepIfIntermed(500);

		for(let i=0; i<200; i++)
		{
			[minD, maxD] = step(world, minD, maxD);

			await AdventOfCode.outputIntermed(tostr2(world, minD, maxD));
			await AdventOfCode.sleepIfIntermed(50);
		}

		let count = Object.values(world).filter(p => p).length;

		AdventOfCode.output(DAY, PROBLEM, count.toString());
	}

	function tostr1(world: {[_:number]:boolean}, minD: number, maxD: number): string
	{
		let str = "";

		for(let d=minD; d<=maxD; d++)
		{
			str += "Depth "+d+":\n";
			for(let y=0; y<5; y++)
			{
				for(let x=0; x<5; x++)
				{
					str += (world[id(x, y, d)] === true) ? "#" : ".";
				}
				str += "\n";
			}
			str += "\n";
		}
		return str;
	}

	function tostr2(world: {[_:number]:boolean}, minD: number, maxD: number): string
	{
		let str = "";

		for (let y = 0; y<11*6; y++)
		{
			for (let x = 0; x<19*6; x++)
			{
				let mx = x%6;
				let my = y%6;
				if (mx===5 || my === 5) str += " "
				else if (mx===2 && my === 2) str += "?"
				else
				{
					let md = Math.floor(y/6) * 19 + Math.floor(x/6) - Math.floor((19*11)/2);

					str += (world[id(mx, my, md)] === true) ? "#" : ".";
				}
			}
			str += "\n";
		}
		return str;
	}

	function id(x: number, y: number, depth: number): number
	{
		return depth*100 + x*10 + y;
	}

	function step(world: {[_:number]:boolean}, minD: number, maxD: number): [number, number]
	{
		let newminD = minD;
		let newmaxD = minD;

		let diff: [number, boolean, number][] = [];

		for(let d=minD; d<=maxD; d++)
		for(let y=0;    y<5;     y++)
		for(let x=0;    x<5;     x++)
		{
			if (x === 2 && y === 2) continue;

			let me_id = id(x, y, d);
			let me = (world[me_id] === true);

			let adjac = 0;
			if (x>0 && world[id(x-1, y,   d)] === true) adjac++;
			if (y>0 && world[id(x,   y-1, d)] === true) adjac++;
			if (x<4 && world[id(x+1, y,   d)] === true) adjac++;
			if (y<4 && world[id(x,   y+1, d)] === true) adjac++;

			if (x === 0 && world[id(1, 2, d-1)] === true) adjac++; // WEST  OUTER
			if (y === 0 && world[id(2, 1, d-1)] === true) adjac++; // NORTH OUTER
			if (x === 4 && world[id(3, 2, d-1)] === true) adjac++; // EAST  OUTER
			if (y === 4 && world[id(2, 3, d-1)] === true) adjac++; // SOUTH OUTER

			if (x === 2 && y === 1) // NORTH INNER
			{
				if (world[id(0, 0, d+1)] === true) adjac++;
				if (world[id(1, 0, d+1)] === true) adjac++;
				if (world[id(2, 0, d+1)] === true) adjac++;
				if (world[id(3, 0, d+1)] === true) adjac++;
				if (world[id(4, 0, d+1)] === true) adjac++;
			}
			if (x === 3 && y === 2) // EAST INNER
			{
				if (world[id(4, 0, d+1)] === true) adjac++;
				if (world[id(4, 1, d+1)] === true) adjac++;
				if (world[id(4, 2, d+1)] === true) adjac++;
				if (world[id(4, 3, d+1)] === true) adjac++;
				if (world[id(4, 4, d+1)] === true) adjac++;
			}
			if (x === 2 && y === 3) // SOUTH INNER
			{
				if (world[id(0, 4, d+1)] === true) adjac++;
				if (world[id(1, 4, d+1)] === true) adjac++;
				if (world[id(2, 4, d+1)] === true) adjac++;
				if (world[id(3, 4, d+1)] === true) adjac++;
				if (world[id(4, 4, d+1)] === true) adjac++;
			}
			if (x === 1 && y === 2) // WEST INNER
			{
				if (world[id(0, 0, d+1)] === true) adjac++;
				if (world[id(0, 1, d+1)] === true) adjac++;
				if (world[id(0, 2, d+1)] === true) adjac++;
				if (world[id(0, 3, d+1)] === true) adjac++;
				if (world[id(0, 4, d+1)] === true) adjac++;
			}

			if (me && adjac !== 1) diff.push([me_id, false, d]); // A bug dies (becoming an empty space) unless there is exactly one bug adjacent to it.
			else if (!me && (adjac === 1 || adjac === 2)) diff.push([me_id, true, d]); // An empty space becomes infested with a bug if exactly one or two bugs are adjacent to it.
		}

		for (const [id, val, depth] of diff)
		{
			world[id] = val;
			if (depth <= newminD) newminD = depth-1;
			if (depth >= newmaxD) newmaxD = depth+1;
		}

		return [newminD, newmaxD];
	}
}
Result: 1916


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