2018-12-18

# Day 18: Settlers of The North Pole

Description:
--- Day 18: Settlers of The North Pole ---

On the outskirts of the North Pole base construction project, many Elves are collecting lumber.

The lumber collection area is 50 acres by 50 acres; each acre can be either open ground (.), trees (|), or a lumberyard (#). You take a scan of the area (your puzzle input).

Strange magic is at work here: each minute, the landscape looks entirely different. In exactly one minute, an open acre can fill with trees, a wooded acre can be converted to a lumberyard, or a lumberyard can be cleared to open ground (the lumber having been sent to other projects).

The change to each acre is based entirely on the contents of that acre as well as the number of open, wooded, or lumberyard acres adjacent to it at the start of each minute. Here, "adjacent" means any of the eight acres surrounding that acre. (Acres on the edges of the lumber collection area might have fewer than eight adjacent acres; the missing acres aren't counted.)

In particular:

An open acre will become filled with trees if three or more adjacent acres contained trees. Otherwise, nothing happens.
An acre filled with trees will become a lumberyard if three or more adjacent acres were lumberyards. Otherwise, nothing happens.
An acre containing a lumberyard will remain a lumberyard if it was adjacent to at least one other lumberyard and at least one acre containing trees. Otherwise, it becomes open.

These changes happen across all acres simultaneously, each of them using the state of all acres at the beginning of the minute and changing to their new form by the end of that same minute. Changes that happen during the minute don't affect each other.

For example, suppose the lumber collection area is instead only 10 by 10 acres with this initial configuration:

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

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

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

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

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

After 5 minutes:
....|||#..
...||||#..
.|.##||||.
..####|||#
.|.###||#|
|||###||||
||||||||||
||||||||||
||||||||||
||||||||||

After 6 minutes:
...||||#..
...||||#..
.|.###|||.
..#.##|||#
|||#.##|#|
|||###||||
||||#|||||
||||||||||
||||||||||
||||||||||

After 7 minutes:
...||||#..
..||#|##..
.|.####||.
||#..##||#
||##.##|#|
|||####|||
|||###||||
||||||||||
||||||||||
||||||||||

After 8 minutes:
..||||##..
..|#####..
|||#####|.
||#...##|#
||##..###|
||##.###||
|||####|||
||||#|||||
||||||||||
||||||||||

After 9 minutes:
..||###...
.||#####..
||##...##.
||#....###
|##....##|
||##..###|
||######||
|||###||||
||||||||||
||||||||||

After 10 minutes:
.||##.....
||###.....
||##......
|##.....##
|##.....##
|##....##|
||##.####|
||#####|||
||||#|||||
||||||||||

After 10 minutes, there are 37 wooded acres and 31 lumberyards. Multiplying the number of wooded acres by the number of lumberyards gives the total resource value after ten minutes: 37 * 31 = 1147.

What will the total resource value of the lumber collection area be after 10 minutes?

--- Part Two ---

This important natural resource will need to last for at least thousands of years. Are the Elves collecting this lumber sustainably?

What will the total resource value of the lumber collection area be after 1000000000 minutes?

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

Part 1:
``````enum Field { Empty, Tree, Lumber }

Dictionary<char, Field> mapping = new Dictionary<char, Field>
{
{'.', Field.Empty},
{'|', Field.Tree},
{'#', Field.Lumber},
};

int Width;
int Height;
Field[,] Map;

void Main()
{
//\$"Initial state:".Dump();Dump();

int i;
for (i = 0; i < 10; i++)
{
Tick();
//\$"After {i+1} minutes:".Dump();Dump();
}

\$"After {i} ticks there are {FullCount(Field.Tree)} trees and {FullCount(Field.Lumber)} lumbers, the score is {FullCount(Field.Tree) * FullCount(Field.Lumber)}".Dump();
}

void Dump()
{
StringBuilder b = new StringBuilder();
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
if (Map[x, y] == Field.Empty) b.Append('.');
else if (Map[x, y] == Field.Tree) b.Append('|');
else if (Map[x, y] == Field.Lumber) b.Append('#');
else throw new Exception(".");
}
b.AppendLine();
}
b.ToString().Dump();
"".Dump();
}

{
Width = input[0].Length;
Height = input.Length;
Map = new Field[Width, Height];
for (var x = 0; x < Width; x++) for (var y = 0; y < Height; y++) Map[x, y] = mapping[input[y][x]];
}

void Tick()
{
var d = new Field[Width,Height];
for (var y = 0; y < Height; y++) for (var x = 0; x < Width; x++) Tick(ref Map, ref d, x, y);
Map=d;
}

void Tick(ref Field[,] src, ref Field[,] dst, int x, int y)
{
if (src[x,y] == Field.Empty)
{
dst[x,y] = (Count(ref src, x, y, Field.Tree) >= 3) ? Field.Tree : Field.Empty;
}
else if (src[x, y] == Field.Tree)
{
dst[x, y] = (Count(ref src, x, y, Field.Lumber) >= 3) ? Field.Lumber : Field.Tree;
}
else if (src[x, y] == Field.Lumber)
{
dst[x, y] = (Count(ref src, x, y, Field.Lumber) >= 1 && Count(ref src, x, y, Field.Tree) >= 1) ? Field.Lumber : Field.Empty;
}
}

int Count(ref Field[,] fld, int x, int y, Field s)
{
int c = 0;
if (x > 0       && y > 0        && fld[x - 1, y - 1] == s) c++;
if (x > 0                       && fld[x - 1, y    ] == s) c++;
if (x > 0       && y < Height-1 && fld[x - 1, y + 1] == s) c++;
if (               y < Height-1 && fld[x    , y + 1] == s) c++;
if (x < Width-1 && y < Height-1 && fld[x + 1, y + 1] == s) c++;
if (x < Width-1                 && fld[x + 1, y    ] == s) c++;
if (x < Width-1 && y > 0        && fld[x + 1, y - 1] == s) c++;
if (               y > 0        && fld[x    , y - 1] == s) c++;
return c;
}

int FullCount(Field f)
{
int c = 0;
for (var x = 0; x < Width; x++) for (var y = 0; y < Height; y++) if (Map[x,y]==f)c++;
return c;
}``````
Result: 536370

Part 2:
``````enum Field { Empty, Tree, Lumber }

Dictionary<char, Field> mapping = new Dictionary<char, Field>
{
{'.', Field.Empty},
{'|', Field.Tree},
{'#', Field.Lumber},
};

int Width;
int Height;
Field[,] Map;

void Main()
{
//\$"Initial state:".Dump();Dump();

var history = new Dictionary<string, int>();

int gen=0;
for (;;)
{
Tick();
gen++;

var hash = Hash();

if (history.ContainsKey(hash))
{
int loop = gen-history[hash];

int remaining = 1000000000;
remaining -= gen;

var off = remaining % loop;

for (int i = 0; i < off; i++) { Tick(); gen++; }

\$"After {gen} ticks there are {FullCount(Field.Tree)} trees and {FullCount(Field.Lumber)} lumbers, the score is {FullCount(Field.Tree) * FullCount(Field.Lumber)} and the state loops every {loop} generations".Dump();

return;
}
else
{
history[hash] = gen;
}
}

}

string Hash()
{
StringBuilder b = new StringBuilder();
for (int y = 0; y < Height; y++) for (int x = 0; x < Width; x++) b.Append((char)('0'+(int)Map[x, y]));
return b.ToString();
}

void Dump()
{
StringBuilder b = new StringBuilder();
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
if (Map[x, y] == Field.Empty) b.Append('.');
else if (Map[x, y] == Field.Tree) b.Append('|');
else if (Map[x, y] == Field.Lumber) b.Append('#');
else throw new Exception(".");
}
b.AppendLine();
}
b.ToString().Dump();
"".Dump();
}

{
Width = input[0].Length;
Height = input.Length;
Map = new Field[Width, Height];
for (var x = 0; x < Width; x++) for (var y = 0; y < Height; y++) Map[x, y] = mapping[input[y][x]];
}

void Tick()
{
var d = new Field[Width,Height];
for (var y = 0; y < Height; y++) for (var x = 0; x < Width; x++) Tick(ref Map, ref d, x, y);
Map=d;
}

void Tick(ref Field[,] src, ref Field[,] dst, int x, int y)
{
if (src[x,y] == Field.Empty)
{
dst[x,y] = (Count(ref src, x, y, Field.Tree) >= 3) ? Field.Tree : Field.Empty;
}
else if (src[x, y] == Field.Tree)
{
dst[x, y] = (Count(ref src, x, y, Field.Lumber) >= 3) ? Field.Lumber : Field.Tree;
}
else if (src[x, y] == Field.Lumber)
{
dst[x, y] = (Count(ref src, x, y, Field.Lumber) >= 1 && Count(ref src, x, y, Field.Tree) >= 1) ? Field.Lumber : Field.Empty;
}
}

int Count(ref Field[,] fld, int x, int y, Field s)
{
int c = 0;
if (x > 0       && y > 0        && fld[x - 1, y - 1] == s) c++;
if (x > 0                       && fld[x - 1, y    ] == s) c++;
if (x > 0       && y < Height-1 && fld[x - 1, y + 1] == s) c++;
if (               y < Height-1 && fld[x    , y + 1] == s) c++;
if (x < Width-1 && y < Height-1 && fld[x + 1, y + 1] == s) c++;
if (x < Width-1                 && fld[x + 1, y    ] == s) c++;
if (x < Width-1 && y > 0        && fld[x + 1, y - 1] == s) c++;
if (               y > 0        && fld[x    , y - 1] == s) c++;
return c;
}

int FullCount(Field f)
{
int c = 0;
for (var x = 0; x < Width; x++) for (var y = 0; y < Height; y++) if (Map[x,y]==f)c++;
return c;
}``````
Result: 190512

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