public InputData GetParsedModel(string input) { var result = new InputData(); var lines = input.Split('\n'); var firstLineValues = lines[0].Split(' '); result.RowsNumber = Int32.Parse(firstLineValues[0]); result.ColumnsNumber = Int32.Parse(firstLineValues[1]); result.Map = new bool[result.RowsNumber, result.ColumnsNumber]; for (var i = 0; i < result.RowsNumber; i++) { for(var j =0; j < result.ColumnsNumber; j++) { if (lines[i + 1][j] == CellToBePaintedChar) { result.Map[i, j] = true; } } } return result; }
private Command GetBestCommandForPaintedCell(InputData inputData, int rowNumber, int columnNumber) { var command = new PaintLineCommand { Map = inputData.Map, StartPoint = new Point { RowNumber = rowNumber, ColumnNumber = columnNumber }, EndPoint = new Point { RowNumber = rowNumber, ColumnNumber = columnNumber } }; bool isNextRowShouldBeChecked = true; bool isNextColumnShouldBeChecked = true; var lineLength = 1; var rowsPainted = 0; var columnsPainted = 0; while (isNextRowShouldBeChecked || isNextColumnShouldBeChecked) { isNextRowShouldBeChecked = isNextRowShouldBeChecked && rowNumber + lineLength < inputData.RowsNumber && inputData.Map[rowNumber + lineLength, columnNumber]; if (isNextRowShouldBeChecked) { rowsPainted++; } isNextColumnShouldBeChecked = isNextColumnShouldBeChecked && columnNumber + lineLength < inputData.ColumnsNumber && inputData.Map[rowNumber, columnNumber + lineLength]; if (isNextColumnShouldBeChecked) { columnsPainted++; } lineLength++; } if(rowsPainted > columnsPainted) { command.EndPoint.RowNumber = rowNumber + rowsPainted; } else { command.EndPoint.ColumnNumber = columnNumber + columnsPainted; } return command; }
public OutputData GetOutputData(InputData inputData) { var result = new OutputData(); // TODO: improve solver for (var i = 0; i < inputData.RowsNumber; i++) { for (var j = 0; j < inputData.ColumnsNumber; j++) { if (inputData.Map[i, j]) { var currentCommand = GetBestCommandForPaintedCell(inputData, i, j); // To erase figure from map currentCommand.Undo(); result.Commands.Add(currentCommand); } } } return result; }
public string GetSerrializedInput(InputData inputData, OutputData outputData) { var map = new bool[inputData.RowsNumber, inputData.ColumnsNumber]; foreach (var command in outputData.Commands) { command.Map = map; command.Do(); } var result = new StringBuilder(); for (var i = 0; i < inputData.RowsNumber; i++) { for (var j = 0; j < inputData.ColumnsNumber; j++) { result.Append(map[i, j] ? InputDataParser.CellToBePaintedChar : InputDataParser.CellBlankChar); } result.AppendLine(); } return result.ToString(); }
public int GetScore(InputData inputData, OutputData outputData) { return inputData.ColumnsNumber * inputData.RowsNumber - outputData.Commands.Count; }