about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2021-12-11 00:40:11 -0500
committerBen Harris <ben@tilde.team>2021-12-11 00:40:11 -0500
commitc168e0b9a2e6054583c1d44a80b154a689a17fee (patch)
treef16957b22e9ee204979725a3b2bef34ed11c3968
parentfabcc5695119b0f49da3b874cce29430f222c710 (diff)
day 11
-rw-r--r--aoc2021.test/DayTests.cs2
-rw-r--r--aoc2021/Day11.cs61
-rw-r--r--aoc2021/input/day11.in10
-rw-r--r--aoc2021/input/test11.in10
4 files changed, 83 insertions, 0 deletions
diff --git a/aoc2021.test/DayTests.cs b/aoc2021.test/DayTests.cs
index 2795d7c..5d22b13 100644
--- a/aoc2021.test/DayTests.cs
+++ b/aoc2021.test/DayTests.cs
@@ -14,6 +14,7 @@ public class DayTests
     [DataRow(typeof(Day08), "362", "1020159")]
     [DataRow(typeof(Day09), "478", "1327014")]
     [DataRow(typeof(Day10), "288291", "820045242")]
+    [DataRow(typeof(Day11), "1613", "510")]
     public void CheckAllDays(Type dayType, string part1, string part2)
     {
         var s = Stopwatch.StartNew();
@@ -54,6 +55,7 @@ public class DayTests
     [DataRow(typeof(Day08), "26", "61229")]
     [DataRow(typeof(Day09), "15", "1134")]
     [DataRow(typeof(Day10), "26397", "288957")]
+    [DataRow(typeof(Day11), "1656", "195")]
     public void CheckTestInputs(Type dayType, string part1, string part2)
     {
         Day.UseTestInput = true;
diff --git a/aoc2021/Day11.cs b/aoc2021/Day11.cs
new file mode 100644
index 0000000..f5795dd
--- /dev/null
+++ b/aoc2021/Day11.cs
@@ -0,0 +1,61 @@
+namespace aoc2021;
+
+/// <summary>
+/// Day 11: <see href="https://adventofcode.com/2021/day/11"/>
+/// </summary>
+public sealed class Day11 : Day
+{
+    private int _currentAnswer;
+    private readonly int _flashesAfter100, _totalTurns;
+    private readonly int[][] _octopusField;
+    
+    public Day11() : base(11, "Dumbo Octopus")
+    {
+        _octopusField = Input.Select(line => line.Select(c => int.Parse($"{c}")).ToArray()).ToArray();
+
+        while (true)
+        {
+            _totalTurns++;
+            
+            // increment all octopuses
+            for (var row = 0; row < _octopusField.Length; row++)
+            for (var col = 0; col < _octopusField[row].Length; col++)
+                _octopusField[row][col]++;
+            
+            // flash any that exceeded 10
+            for (var row = 0; row < _octopusField.Length; row++)
+            for (var col = 0; col < _octopusField[row].Length; col++)
+                if (_octopusField[row][col] == 10)
+                    FlashAt(row, col);
+
+            var done = true;
+            for (var row = 0; row < _octopusField.Length; row++)
+            for (var col = 0; col < _octopusField[row].Length; col++)
+                if (_octopusField[row][col] == -1)
+                    _octopusField[row][col] = 0;
+                else
+                    done = false;
+            
+            if (_totalTurns == 100) _flashesAfter100 = _currentAnswer;
+            if (done) break;
+        }
+    }
+
+    private void FlashAt(int r, int c)
+    {
+        _currentAnswer++;
+        _octopusField[r][c] = -1;
+        foreach (var rr in new[] { -1, 0, 1 }.Select(dr => dr + r))
+        foreach (var cc in new[] { -1, 0, 1 }.Select(dc => dc + c))
+            if (0 <= rr && rr < _octopusField.Length && 0 <= cc && cc < _octopusField[0].Length && _octopusField[rr][cc] != -1)
+            {
+                _octopusField[rr][cc]++;
+                if (_octopusField[rr][cc] >= 10)
+                    FlashAt(rr, cc);
+            }
+    }
+
+    public override string Part1() => $"{_flashesAfter100}";
+
+    public override string Part2() => $"{_totalTurns}";
+}
diff --git a/aoc2021/input/day11.in b/aoc2021/input/day11.in
new file mode 100644
index 0000000..652096a
--- /dev/null
+++ b/aoc2021/input/day11.in
@@ -0,0 +1,10 @@
+3322874652
+5636588857
+7755117548
+5854121833
+2856682477
+3124873812
+1541372254
+8634383236
+2424323348
+2265635842
\ No newline at end of file
diff --git a/aoc2021/input/test11.in b/aoc2021/input/test11.in
new file mode 100644
index 0000000..d7b4395
--- /dev/null
+++ b/aoc2021/input/test11.in
@@ -0,0 +1,10 @@
+5483143223
+2745854711
+5264556173
+6141336146
+6357385478
+4167524645
+2176841721
+6882881134
+4846848554
+5283751526
\ No newline at end of file