public int DetermineWaterVolumeWhenSpringRunsDry(string filePath) { Geology geology = CreateStrata(filePath); var springX = 500; var springY = 0; // fill with water GoDown(springX, springY, geology); // count spaces with water var waterVolume = 0; for (int y = geology.MinY; y < geology.Strata.GetLength(1); y++) { for (int x = 0; x < geology.Strata.GetLength(0); x++) { if (geology.Strata[x, y] == 'W') { waterVolume++; } } } return(waterVolume); }
private void GoDown(int x, int y, Geology geology) { geology.Strata[x, y] = '|'; while (geology.Strata[x, y + 1] != '#' && geology.Strata[x, y + 1] != 'W') { y++; if (y > geology.MaxY) { return; } geology.Strata[x, y] = '|'; } ; do { bool goDownLeft = false; bool goDownRight = false; // find boundaries int minX; for (minX = x; minX >= 0; minX--) { if (SpaceTaken(minX, y + 1, geology.Strata) == false) { goDownLeft = true; break; } geology.Strata[minX, y] = '|'; if (SpaceTaken(minX - 1, y, geology.Strata)) { break; } } int maxX; for (maxX = x; maxX < geology.Strata.GetLength(0); maxX++) { if (SpaceTaken(maxX, y + 1, geology.Strata) == false) { goDownRight = true; break; } geology.Strata[maxX, y] = '|'; if (SpaceTaken(maxX + 1, y, geology.Strata)) { break; } } // handle water falling if (goDownLeft) { if (geology.Strata[minX, y] != '|') { GoDown(minX, y, geology); } } if (goDownRight) { if (geology.Strata[maxX, y] != '|') { GoDown(maxX, y, geology); } } if (goDownLeft || goDownRight) { return; } // fill row for (int a = minX; a < maxX + 1; a++) { geology.Strata[a, y] = 'W'; } y--; }while (true); }