private Grid2 <Octopus> LoadGrid() { string[] input = File.ReadAllLines("Day11Input.txt"); Grid2 <Octopus> grid = new Grid2 <Octopus>(input[0].Length, input.Length); foreach (Point2 point in grid.Points) { grid[point] = new Octopus(input[point.Y][point.X] - '0'); } return(grid); }
private void TryFlash(Grid2 <Octopus> grid, Point2 point) { Octopus o = grid[point]; if (o.Energy > 9 && !o.HasFlashed) { o.HasFlashed = true; foreach (Point2 neighbor in point.Surrounding(grid.Bounds)) { grid[neighbor].Energy++; TryFlash(grid, neighbor); } } }
public static void Run() { var inputs = File.ReadAllLines("Day11Input.txt"); Octopus[,] grid = new Octopus[10, 10]; //Setup the grid for (int i = 0; i < inputs.Length; i++) { for (int j = 0; j < inputs.Length; j++) { grid[i, j] = new Octopus(inputs[i][j]); } } long flashes = 0; int count = 0; while (true) { //set the grid bool stepComplete = false; for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { if (grid[j, k].hasFired) { grid[j, k].energy = 0; } grid[j, k].energy++; grid[j, k].hasFired = false; } } while (!stepComplete) { stepComplete = true; for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { if (!grid[j, k].hasFired && grid[j, k].energy > 9) { //Octopus hasn't fired and energy above 9 - take cover! flashes += FireOctopus(j, k, grid); stepComplete = false; } } } } count++; if (count == 100) { Console.WriteLine("Part 1 - " + flashes); } bool inSync = true; for (int i = 0; i < inputs.Length; i++) { for (int j = 0; j < inputs.Length; j++) { if (!grid[i, j].hasFired) { inSync = false; break; } } if (!inSync) { break; } } if (inSync) { break; } } Console.WriteLine("Part 2 - " + count); }