string filePath = @"Squares.txt";
List<List<int>> squares = new();
List<List<string>> squaresString = new();
// Getting squares.
File.ReadAllLines(filePath).ToList().ForEach(squareString =>
squaresString.Add(squareString.Split(' ').ToList()));
squaresString.ForEach(squareString =>
{
List<int> square = new();
squareString.ForEach(number => square.Add(int.Parse(number)));
squares.Add(square);
});
// Searching for squares matching the green condition.
List<List<int>> greenSquares = new();
int first = 0, second = 1, third = 2, fourth = 3;
const int maxNumber = 11, minNumber = 0;
bool isNewCheck4, isNewCheck3, isNewCheck2;
while(first != maxNumber || second != maxNumber - 1 || third != maxNumber - 2 ||
fourth != maxNumber - 3)
{
isNewCheck2 = isNewCheck3 = isNewCheck4 = true;
if (DoSquaresFitToGreenCondition(squares[first], squares[second], squares[third],
squares[fourth]))
{
greenSquares.Add(new List<int> { first, second, third, fourth });
}
while (isNewCheck4 || fourth == third || fourth == second || fourth == first)
{
isNewCheck4 = false;
if (fourth == maxNumber)
{
fourth = minNumber;
while (isNewCheck3 || third == second || third == first)
{
isNewCheck3 = false;
if (third == maxNumber)
{
third = minNumber;
while (isNewCheck2 || second == first)
{
isNewCheck2 = false;
if (second == maxNumber)
{
second = minNumber;
first++;
}
else
second++;
}
}
else
third++;
}
}
else
fourth++;
}
}
if (DoSquaresFitToGreenCondition(squares[first], squares[second], squares[third],
squares[fourth]))
{
greenSquares.Add(new List<int> { first, second, third, fourth });
}
// Grouping into 5 green squares.
List<List<int>> groupGreenSquares = new();
List<int>? upGreenSquare = null, downGreenSquare = null, leftGreenSquare = null,
rightGreenSquare = null, middleGreenSquare = null;
List<int> usingSquares = new();
int cycle = 0;
while ((upGreenSquare == null || downGreenSquare == null || leftGreenSquare == null ||
rightGreenSquare == null) && cycle < greenSquares.Count)
{
middleGreenSquare = greenSquares[cycle++];
rightGreenSquare = leftGreenSquare = upGreenSquare = downGreenSquare = null;
for (int i = 0; i < greenSquares.Count && (upGreenSquare == null ||
downGreenSquare == null || leftGreenSquare == null ||
rightGreenSquare == null); i++)
{
if (upGreenSquare == null &&
middleGreenSquare[0] == greenSquares[i][2] &&
middleGreenSquare[1] == greenSquares[i][3] &&
!usingSquares.Contains(greenSquares[i][0]) &&
!usingSquares.Contains(greenSquares[i][1]) &&
!middleGreenSquare.Contains(greenSquares[i][0]) &&
!middleGreenSquare.Contains(greenSquares[i][1]))
{
upGreenSquare = greenSquares[i];
usingSquares.AddRange(new int[] { upGreenSquare[0], upGreenSquare[1] });
}
else if (downGreenSquare == null &&
middleGreenSquare[2] == greenSquares[i][0] &&
middleGreenSquare[3] == greenSquares[i][1] &&
!usingSquares.Contains(greenSquares[i][2]) &&
!usingSquares.Contains(greenSquares[i][3]) &&
!middleGreenSquare.Contains(greenSquares[i][2]) &&
!middleGreenSquare.Contains(greenSquares[i][3]))
{
downGreenSquare = greenSquares[i];
usingSquares.AddRange(new int[] { downGreenSquare[2], downGreenSquare[3] });
}
else if (leftGreenSquare == null &&
middleGreenSquare[0] == greenSquares[i][1] &&
middleGreenSquare[2] == greenSquares[i][3] &&
!usingSquares.Contains(greenSquares[i][0]) &&
!usingSquares.Contains(greenSquares[i][2]) &&
!middleGreenSquare.Contains(greenSquares[i][0]) &&
!middleGreenSquare.Contains(greenSquares[i][2]))
{
leftGreenSquare = greenSquares[i];
usingSquares.AddRange(new int[] { leftGreenSquare[0], leftGreenSquare[2] });
}
else if (rightGreenSquare == null &&
middleGreenSquare[1] == greenSquares[i][0] &&
middleGreenSquare[3] == greenSquares[i][2] &&
!usingSquares.Contains(greenSquares[i][1]) &&
!usingSquares.Contains(greenSquares[i][3]) &&
!middleGreenSquare.Contains(greenSquares[i][1]) &&
!middleGreenSquare.Contains(greenSquares[i][3]))
{
rightGreenSquare = greenSquares[i];
usingSquares.AddRange(new int[] { rightGreenSquare[1], rightGreenSquare[3] });
}
}
}
groupGreenSquares.AddRange(new List<List<int>>{ upGreenSquare, leftGreenSquare,
middleGreenSquare, rightGreenSquare, downGreenSquare});
}
static bool DoSquaresFitToGreenCondition(List<int> fisrt, List<int> second,
List<int> third, List<int> fourth)
{
return (fisrt[3] + second[2] + third[1] + fourth[0]) == 10;
}