string[] input = File.ReadAllLines("../../../../Input.in");
int xSize = input[0].Length;
int ySize = input.Length;
char[,] map1 = new char[xSize, ySize];
char[,] map2 = new char[xSize, ySize];
Dictionary<string, char> mapSouthMovement = new Dictionary<string, char>();
Dictionary<string, char> mapEastMovement = new Dictionary<string, char>();
InitializeMap();
int numSteps = 0;
bool cucumbersMovedEast = false;
bool cucumbersMovedSouth = false;
do
{
cucumbersMovedEast = false;
cucumbersMovedSouth = false;
for (int step = 1; step <= 2; step++)
{
for (int y = 0; y < ySize; y++)
{
for (int x = 0; x < xSize; x++)
{
if (step == 1)
{// move east
if (mapSouthMovement[x + "," + y] == '>')
{
if (x < xSize - 1)
{
if (mapSouthMovement[((x + 1) + "," + y)] == '.')
{
mapEastMovement.Add(((x + 1) + "," + y), '>');
mapEastMovement.Add((x + "," + y), '.');
cucumbersMovedEast = true;
}
else
{
mapEastMovement.Add((x + "," + y), '>');
}
}
else if (x == xSize - 1)
{
if (mapSouthMovement[("0," + y)] == '.')
{
if (mapEastMovement.ContainsKey(("0," + y)) && mapEastMovement[("0," + y)] == '.')
mapEastMovement[("0," + y)] = '>';
else
{
mapEastMovement.Add(("0," + y), '>');
}
mapEastMovement.Add((x + "," + y), '.');
cucumbersMovedEast = true;
}
else
{
mapEastMovement.Add((x + "," + y), '>');
}
}
}
else
{
if (!mapEastMovement.ContainsKey(x + "," + y))
{
mapEastMovement.Add((x + "," + y), mapSouthMovement[(x + "," + y)]);
}
}
}
if (step == 2)
{// move south
if (mapEastMovement[x + "," + y] == 'v')
{
if (y < ySize - 1)
{
if (mapEastMovement[(x + "," + (y + 1))] == '.')
{
mapSouthMovement.Add((x + "," + (y + 1)), 'v');
mapSouthMovement.Add((x + "," + y), '.');
cucumbersMovedSouth = true;
}
else
{
mapSouthMovement.Add((x + "," + y), 'v');
}
}
else if (y == ySize - 1)
{
if (mapEastMovement[(x + ",0")] == '.')
{
if (mapSouthMovement.ContainsKey((x + ",0")) && mapSouthMovement[(x + ",0")] == '.')
mapSouthMovement[(x + ",0")] = 'v';
else
mapSouthMovement.Add((x + ",0"), 'v');
mapSouthMovement.Add((x + "," + y), '.');
cucumbersMovedSouth = true;
}
else
{
mapSouthMovement.Add((x + "," + y), 'v');
}
}
}
else
{
if (!mapSouthMovement.ContainsKey(x + "," + y))
{
mapSouthMovement.Add((x + "," + y), mapEastMovement[(x + "," + y)]);
}
}
}
}
}
numSteps++;
if (step == 1)
{
mapSouthMovement.Clear();
if (!cucumbersMovedEast)
{
Console.WriteLine("Stopped Moving East: " + numSteps / 2);
}
}
if (step == 2)
{
mapEastMovement.Clear();
if (!cucumbersMovedSouth)
{
Console.WriteLine("Stopped Moving South: " + numSteps / 2);
}
}
}
} while (cucumbersMovedEast || cucumbersMovedSouth);
void InitializeMap()
{
for (int y = 0; y < ySize; y++)
{
for (int x = 0; x < xSize; x++)
{
mapSouthMovement.Add((x + "," + y), input[y][x]);
}
}
}