Created
October 21, 2015 17:11
Solution for /r/dailyprogrammer 10/21/2015
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Daily | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string input = GetInput(); | |
char[][] lines = input.Split('\n').Select(line => line.Trim().ToCharArray()).ToArray(); | |
// Assume that 0,0 is always the + for layer 0 | |
// Flood fill layer 0, starting from 1,1 | |
FloodFill(1, 1, ref lines, '#'); | |
for (int i = 1; i < lines.Length; i++) | |
{ | |
for (int j = 1; j < lines[0].Length; j++) | |
{ | |
if(lines[i-1][j-1] == '#' && lines[i][j] == '|' && j + 1 < lines[0].Length) | |
{ | |
FloodFill(i, j + 1, ref lines, '='); | |
} | |
} | |
} | |
for (int i = 1; i < lines.Length; i++) | |
{ | |
for (int j = 1; j < lines[0].Length; j++) | |
{ | |
if (lines[i - 1][j - 1] == '=' && lines[i][j] == '|' && j + 1 < lines[0].Length) | |
{ | |
FloodFill(i, j + 1, ref lines, '-'); | |
} | |
} | |
} | |
for (int i = 1; i < lines.Length; i++) | |
{ | |
for (int j = 1; j < lines[0].Length; j++) | |
{ | |
if (lines[i - 1][j - 1] == '-' && lines[i][j] == '|' && j + 1 < lines[0].Length) | |
{ | |
FloodFill(i, j + 1, ref lines, '.'); | |
} | |
} | |
} | |
Print(lines); | |
Console.ReadKey(); | |
} | |
private static void FloodFill(int startRow, int startColumn, ref char[][] toFill, char fillChar) | |
{ | |
if(toFill[startRow][startColumn] == ' ') | |
toFill[startRow][startColumn] = fillChar; | |
else | |
{ | |
return; | |
} | |
if(startRow + 1 < toFill.Length && !toFill[startRow + 1][startColumn].In(fillChar, '+', '|', '-')) | |
{ | |
FloodFill(startRow + 1, startColumn, ref toFill, fillChar); | |
} | |
if(startRow - 1 > -1 && !toFill[startRow - 1][startColumn].In(fillChar, '+', '|', '-')) | |
{ | |
FloodFill(startRow - 1, startColumn, ref toFill, fillChar); | |
} | |
if(startColumn + 1 < toFill[0].Length && !toFill[startRow][startColumn + 1].In(fillChar, '+', '|', '-')) | |
{ | |
FloodFill(startRow, startColumn + 1, ref toFill, fillChar); | |
} | |
if(startColumn - 1 > -1 && !toFill[startRow][startColumn - 1].In(fillChar, '+', '|', '-')) | |
{ | |
FloodFill(startRow, startColumn - 1, ref toFill, fillChar); | |
} | |
} | |
private static void Print(char[][] lines) | |
{ | |
for (int i = 0; i < lines.Length; i++) | |
{ | |
for (int j = 0; j < lines[0].Length; j++) | |
{ | |
Console.Write(lines[i][j]); | |
} | |
Console.WriteLine(); | |
} | |
Console.WriteLine('\n'); | |
} | |
private static string GetInput() | |
{ | |
return @"+--------------------------------------------------------------+ | |
| | | |
| +-------------------------------+ +-------+ | | |
| | | | | | | |
| | | | | | | |
| | +----------------+ | | | | | |
| | | | | +-------+ | | |
| | | | | | | |
| | | | | +-------+ | | |
| | +----------------+ | | | | | |
| | | | | | | |
| | | | | | | |
| +-------------------------------+ +-------+ | | |
| | | |
+--------------------------------------------------------------+".Trim(); | |
} | |
private static string GetInput2() | |
{ | |
return @"+-----------------------------------------------------------------------+ | |
| +--------------------------------------------------------------+ | | |
| | +-----------------------------------------------------+ | | | |
| | | +-----------------------------------------+ | | | | |
| | | | +---------------------------+ | | | | | |
| | | | | | | | | | | |
| | | | | | | | | | | |
| | | | | | | | | | | |
| | | | +---------------------------+ | | | | | |
| | | | | | | | | |
| | | +-----------------------------------------+ | | | | |
| | | | | | | |
| | | | | | | |
| | +-----------------------------------------------------+ | | | |
| | | | | |
| +--------------------------------------------------------------+ | | |
| | | |
| | | |
| | | |
+-----------------------------------------------------------------------+".Trim(); | |
} | |
} | |
public static class Helpers | |
{ | |
public static bool In<T>(this T obj, params T[] args) | |
{ | |
return args.Contains(obj); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment