day 11
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2021-12-11 00:40:11 -05:00
parent fabcc56951
commit c168e0b9a2
4 changed files with 83 additions and 0 deletions

View File

@ -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;

61
aoc2021/Day11.cs Normal file
View File

@ -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}";
}

10
aoc2021/input/day11.in Normal file
View File

@ -0,0 +1,10 @@
3322874652
5636588857
7755117548
5854121833
2856682477
3124873812
1541372254
8634383236
2424323348
2265635842

10
aoc2021/input/test11.in Normal file
View File

@ -0,0 +1,10 @@
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526