chore: add comments to tilt dish to north method

This commit is contained in:
Stevan Freeborn
2023-12-20 20:45:02 -06:00
parent 24c171c790
commit e18b944893
+15
View File
@@ -47,27 +47,42 @@ public class Dish(
private List<List<char>> TiltDishToNorth(List<List<char>> rows) private List<List<char>> TiltDishToNorth(List<List<char>> rows)
{ {
// create a copy of the rows which we can modify
var modifiedRows = rows.ToList(); var modifiedRows = rows.ToList();
var numOfColumns = modifiedRows[0].Count; var numOfColumns = modifiedRows[0].Count;
var numOfRows = modifiedRows.Count; var numOfRows = modifiedRows.Count;
// iterate over the columns from left to right
for (var currentColumnIndex = 0; currentColumnIndex < numOfColumns; currentColumnIndex++) for (var currentColumnIndex = 0; currentColumnIndex < numOfColumns; currentColumnIndex++)
{ {
// iterate over the rows from top to bottom
for (var currentRowIndex = 0; currentRowIndex < numOfRows; currentRowIndex++) for (var currentRowIndex = 0; currentRowIndex < numOfRows; currentRowIndex++)
{ {
// get the current symbol
var current = modifiedRows[currentRowIndex][currentColumnIndex]; var current = modifiedRows[currentRowIndex][currentColumnIndex];
// if the current symbol is an empty space
if (current is EmptySpaceSymbol) if (current is EmptySpaceSymbol)
{ {
// iterate over the rows below the current row
// starting from the row after the current row
for (var i = currentRowIndex + 1; i < numOfRows; i++) for (var i = currentRowIndex + 1; i < numOfRows; i++)
{ {
// get the next symbol in the current column
var next = modifiedRows[i][currentColumnIndex]; var next = modifiedRows[i][currentColumnIndex];
// if the next symbol is a square rock
// we can stop iterating over the rows below
// because the square rock blocks moving
// any round rock to fill the empty space
if (next is SquareRockSymbol) if (next is SquareRockSymbol)
{ {
break; break;
} }
// if the next symbol is a round rock
// we can move the round rock to the empty space
// and stop iterating over the rows below
if (next is RoundRockSymbol) if (next is RoundRockSymbol)
{ {
modifiedRows[currentRowIndex][currentColumnIndex] = RoundRockSymbol; modifiedRows[currentRowIndex][currentColumnIndex] = RoundRockSymbol;