After the million lights incident, the fire code has gotten stricter: now, at most ten thousand lights are allowed. You arrange them in a 100x100 grid.
Never one to let you down, Santa again mails you instructions on the ideal lighting configuration. With so few lights, he says, you'll have to resort to animation.
Read the full puzzle.
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode.Y2015.Day18;
[ProblemName("Like a GIF For Your Yard")]
class Solution : Solver {
public object PartOne(string input) =>
Enumerable.Range(0, 100).Aggregate(Parse(input), (acc, _) => Step(acc, false)).Select(row => row.Sum()).Sum();
public object PartTwo(string input) =>
Enumerable.Range(0, 100).Aggregate(Parse(input), (acc, _) => Step(acc, true)).Select(row => row.Sum()).Sum();
int[][] Step(int[][] input, bool stuck) {
var res = new List<int[]>();
var (crow, ccol) = (input.Length, input[0].Length);
if (stuck) {
input[0][0] = 1;
input[crow - 1][0] = 1;
input[0][ccol - 1] = 1;
input[crow - 1][ccol - 1] = 1;
}
for (var irow = 0; irow < crow; irow++) {
var row = new List<int>();
for (var icol = 0; icol < ccol; icol++) {
if (stuck &&
((icol == 0 && irow == 0) || (icol == ccol - 1 && irow == 0) ||
(icol == 0 && irow == crow - 1) || (icol == ccol - 1 && irow == crow - 1))
) {
row.Add(1);
} else {
var neighbours =
(from d in new(int row, int col)[] { (-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1) }
let irowT = irow + d.row
let icolT = icol + d.col
where irowT >= 0 && irowT < crow && icolT >= 0 && icolT < ccol && input[irowT][icolT] == 1
select 1).Sum();
if (input[irow][icol] == 1) {
row.Add(new[] { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }[neighbours]);
} else {
row.Add(new[] { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }[neighbours]);
}
}
}
res.Add(row.ToArray());
}
return res.ToArray();
}
int[][] Parse(string input) =>(
from line in input.Split('\n')
select
(from ch in line select ch == '#' ? 1 : 0).ToArray()
).ToArray();
}
Please ☆ my repo if you like it!