From a522b93f9b3b3dd80745c37f37d7c74a5f91ae0b Mon Sep 17 00:00:00 2001 From: Parnic Date: Fri, 10 Dec 2021 23:49:32 -0600 Subject: [PATCH] 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. --- advent-of-code-2021.csproj | 3 + inputs/11.txt | 10 +++ src/11.cs | 142 +++++++++++++++++++++++++++++++++++++ src/main.cs | 6 +- 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 inputs/11.txt create mode 100644 src/11.cs diff --git a/advent-of-code-2021.csproj b/advent-of-code-2021.csproj index 6a8a541..b585d03 100644 --- a/advent-of-code-2021.csproj +++ b/advent-of-code-2021.csproj @@ -48,6 +48,9 @@ PreserveNewest + + PreserveNewest + diff --git a/inputs/11.txt b/inputs/11.txt new file mode 100644 index 0000000..328ac4f --- /dev/null +++ b/inputs/11.txt @@ -0,0 +1,10 @@ +3265255276 +1537412665 +7335746422 +6426325658 +3854434364 +8717377486 +4522286326 +6337772845 +8824387665 +6351586484 \ No newline at end of file diff --git a/src/11.cs b/src/11.cs new file mode 100644 index 0000000..c7cf47c --- /dev/null +++ b/src/11.cs @@ -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}"); + } +} diff --git a/src/main.cs b/src/main.cs index afd62e0..2915053 100644 --- a/src/main.cs +++ b/src/main.cs @@ -33,7 +33,11 @@ switch (arg) aoc2021.Day09.Go(); break; - default: + case "10": aoc2021.Day10.Go(); break; + + default: + aoc2021.Day11.Go(); + break; }