Day 11 solution

Pretty happy with this one overall. I tripped myself up on part 2 and caused a huge headache by not realizing that I was passing the already-modified array from part 1 into part 2. I left some code to copy the 2d array because that was my original solution (and it's not straightforward to copy jagged arrays), but then realized that the synchronized flash doesn't happen until after step 100 (at least for my input and the demo input) so we can just account for that in part 2 and pick up where part 1 left off.
This commit is contained in:
2021-12-10 23:49:32 -06:00
parent e61f2bd255
commit a522b93f9b
4 changed files with 160 additions and 1 deletions

View File

@ -48,6 +48,9 @@
<None Update="inputs\10.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="inputs\11.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

10
inputs/11.txt Normal file
View File

@ -0,0 +1,10 @@
3265255276
1537412665
7335746422
6426325658
3854434364
8717377486
4522286326
6337772845
8824387665
6351586484

142
src/11.cs Normal file
View File

@ -0,0 +1,142 @@
namespace aoc2021;
internal class Day11
{
internal static void Go()
{
Logger.Log("Day 11");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/11.txt");
var grid = new byte[lines.Length, lines[0].Length];
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i];
for (int j = 0; j < line.Length; j++)
{
var num = line[j];
grid[i, j] = (byte)char.GetNumericValue(num);
}
}
//var gridCopy = new byte[lines.Length, lines[0].Length];
//Buffer.BlockCopy(grid, 0, gridCopy, 0, grid.Length);
Part1(grid);
Part2(grid);
Logger.Log("");
}
private static IEnumerable<(int, int)> Adjacent(byte[,] grid, int i, int j)
{
if (i > 0)
{
if (j > 0)
{
yield return (i - 1, j - 1);
}
yield return (i - 1, j);
if (j < grid.GetLength(1) - 1)
{
yield return (i - 1, j + 1);
}
}
if (j > 0)
{
yield return (i, j - 1);
}
if (j < grid.GetLength(1) - 1)
{
yield return (i, j + 1);
}
if (i < grid.GetLength(1) - 1)
{
if (j > 0)
{
yield return (i + 1, j - 1);
}
yield return (i + 1, j);
if (j < grid.GetLength(1) - 1)
{
yield return (i + 1, j + 1);
}
}
}
private static void Flash(byte[,] grid, int i, int j, List<(int, int)> flashed)
{
flashed.Add((i, j));
foreach (var pt in Adjacent(grid, i, j))
{
grid[pt.Item1, pt.Item2]++;
if (grid[pt.Item1, pt.Item2] == 10)
{
Flash(grid, pt.Item1, pt.Item2, flashed);
}
}
}
private static void Part1(byte[,] grid)
{
using var t = new Timer();
long numFlashes = 0;
for (int step = 0; step < 100; step++)
{
var flashed = new List<(int, int)>();
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++)
{
grid[i, j]++;
if (grid[i, j] == 10)
{
Flash(grid, i, j, flashed);
}
}
}
foreach (var pt in flashed)
{
grid[pt.Item1, pt.Item2] = 0;
numFlashes++;
}
}
Logger.Log($"part1: {numFlashes}");
}
private static void Part2(byte[,] grid)
{
using var t = new Timer();
int step = 101;
for (; ; step++)
{
var flashed = new List<(int, int)>();
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++)
{
grid[i, j]++;
if (grid[i, j] == 10)
{
Flash(grid, i, j, flashed);
}
}
}
if (flashed.Count == grid.GetLength(0) * grid.GetLength(1))
{
break;
}
foreach (var pt in flashed)
{
grid[pt.Item1, pt.Item2] = 0;
}
}
Logger.Log($"part2: {step}");
}
}

View File

@ -33,7 +33,11 @@ switch (arg)
aoc2021.Day09.Go();
break;
default:
case "10":
aoc2021.Day10.Go();
break;
default:
aoc2021.Day11.Go();
break;
}