Created
September 22, 2015 22:41
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.Text; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using FJOMTesting; | |
using NDesk.Options; | |
namespace FJOMRedditDailyProgrammer | |
{ | |
public class ASCIIArchitect : Tester | |
{ | |
protected bool interactive; | |
protected string testcase; | |
protected string localfile; | |
protected TestCases testcases; | |
protected string[] filecontents; | |
List <string> InOut; | |
protected Random random; | |
public ASCIIArchitect(string[] args) : base(args) | |
{ | |
} | |
public override void Setup() | |
{ | |
base.Setup(); | |
if(interactive) | |
testcases=new TestCases(interactive); | |
//filecontents=System.IO.File.ReadAllLines(localfile); | |
} | |
protected override void Defaults() | |
{ | |
string[] inputs = new string[] {"1\n*" | |
,"3\n * \n *** \n******" | |
,"7\n * \n***\n***\n***\n***\n***\n***" | |
,"3\n **\n*** **\n******" | |
,"7\n*** ***\n*** ** *** ** ***\n*** *************** ***\n*** *************** ***\n*** *************** ***\n**************************\n**************************" | |
}; | |
interactive=false; | |
testcases=new TestCases(interactive,inputs); | |
localfile="233-ASCII-Architect.txt"; | |
random=new Random(); | |
InOut=new List<string>(); | |
} | |
protected override void ParseArgs() | |
{ | |
if (args.Length!=0) | |
{ //Parse Arguments using NDesk.Options | |
var p = new OptionSet() | |
{ | |
{"f|file=", "File with Points", v=>localfile=v } | |
,{"i|interactive", "Interactive mode (off by default)", v=> interactive=true } | |
}; | |
List<string> extraargs; | |
try | |
{ | |
extraargs=p.Parse(args); | |
} | |
catch (OptionException e) | |
{ | |
Console.WriteLine("Error: {0}",e.Message); | |
p.WriteOptionDescriptions(Console.Out); | |
return; | |
} | |
} | |
} | |
protected string HexOutline(string input) | |
{ | |
StringBuilder result=new StringBuilder(); | |
StringBuilder resultIOLines = new StringBuilder(); | |
List<string> lines = new List<string>(input.Split(new char[]{'\n'})); | |
lines.Insert(1,new string(' ',lines[1].Length)); | |
int inheight = Int32.Parse(lines[0])+1; | |
InOut=new List<string>(); | |
for(int i=1;i<=inheight;i++) | |
{ | |
StringBuilder resultline=new StringBuilder(); | |
StringBuilder resultIOLine =new StringBuilder(); | |
for(int j=0;j<lines[i].Length;j++) | |
{ | |
byte thiscell=0; | |
if (lines[i][j]=='*') | |
{ | |
resultIOLine.Append('I'); | |
if(j==0) | |
thiscell+=1; | |
else if(lines[i][j-1]==' ') | |
thiscell+=1; | |
if(j==lines[i].Length-1) | |
thiscell+=2; | |
else if(lines[i][j+1]==' ') | |
thiscell+=2; | |
if(i==inheight) | |
thiscell+=4; | |
else if(lines[i+1][j]==' ') | |
thiscell+=4; | |
if(i==1) | |
thiscell+=8; | |
else if(lines[i-1][j]==' ') | |
thiscell+=8; | |
resultline.Append(thiscell.ToString("X")); | |
} | |
else | |
{ | |
char t='0'; | |
resultIOLine.Append('O'); | |
if(lines[i+1][j]=='*') | |
t='R'; | |
resultline.Append(t.ToString()); | |
} | |
} | |
InOut.Add(resultIOLine.ToString()); | |
result.Append(resultline+Environment.NewLine); | |
} | |
result.Length--; | |
return result.ToString(); | |
} | |
protected string ASCIIOutline(string input) | |
{ | |
StringBuilder result=new StringBuilder(); | |
string[] lines = input.Split(new char[]{'\n'}); | |
for(int i=0;i<lines.Length;i++) | |
{ | |
StringBuilder[] resultlines=new StringBuilder[3]; | |
for(int r=0;r<3;r++) | |
resultlines[r]=new StringBuilder(); | |
int door = (i==(lines.Length-1)) ? random.Next(lines[i].Length-1) : -1 ; | |
for (int j=0;j<lines[i].Length;j++) | |
{ | |
switch(lines[i][j]) | |
{ | |
case 'R' : | |
{ | |
resultlines[0].Append(" "); | |
resultlines[1].Append(" "); | |
char t=' '; | |
if(j==0||lines[i][j-1]!='R') | |
t='/'; | |
resultlines[2].Append(" "+t+" "); | |
t=' '; | |
if(j==lines[i].Length-1||lines[i][j+1]!='R') | |
t='\\'; | |
resultlines[2].Append(t+" "); | |
break; | |
} | |
case '0' : | |
{ | |
resultlines[0].Append(" "); | |
if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append(" o "); | |
else | |
resultlines[1].Append(" "); | |
resultlines[2].Append(" "); | |
break; | |
} | |
case '1' : | |
{ | |
resultlines[0].Append("| "); | |
if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append("| o "); | |
else | |
resultlines[1].Append("| "); | |
resultlines[2].Append("| "); | |
break; | |
} | |
case '2' : | |
{ | |
resultlines[0].Append(" |"); | |
if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append(" o |"); | |
else | |
resultlines[1].Append(" |"); | |
resultlines[2].Append(" |"); | |
break; | |
} | |
case '3' : | |
{ | |
resultlines[0].Append("| |"); | |
if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append("| o |"); | |
else | |
resultlines[1].Append("| |"); | |
resultlines[2].Append("| |"); | |
break; | |
} | |
case '4' : | |
{ | |
resultlines[0].Append(" "); | |
if (door==j) | |
resultlines[1].Append(" | | "); | |
else if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append(" o "); | |
else | |
resultlines[1].Append(" "); | |
resultlines[2].Append("-----"); | |
break; | |
} | |
case '5' : | |
{ | |
resultlines[0].Append("| "); | |
if (door==j) | |
resultlines[1].Append("|| | "); | |
else if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append("| o "); | |
else | |
resultlines[1].Append("| "); | |
resultlines[2].Append("+----"); | |
break; | |
} | |
case '6' : | |
{ | |
resultlines[0].Append(" |"); | |
if (door==j) | |
resultlines[1].Append(" | ||"); | |
else if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append(" o |"); | |
else | |
resultlines[1].Append(" |"); | |
resultlines[2].Append("----+"); | |
break; | |
} | |
case '7' : | |
{ | |
resultlines[0].Append("| |"); | |
if (door==j) | |
resultlines[1].Append("|| ||"); | |
else if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append("| o |"); | |
else | |
resultlines[1].Append("| |"); | |
resultlines[2].Append("+---+"); | |
break; | |
} | |
case '8' : | |
{ | |
resultlines[0].Append("-----"); | |
if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append(" o "); | |
else | |
resultlines[1].Append(" "); | |
resultlines[2].Append(" "); | |
break; | |
} | |
case '9' : | |
{ | |
resultlines[0].Append("+----"); | |
if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append("| o "); | |
else | |
resultlines[1].Append("| "); | |
resultlines[2].Append("| "); | |
break; | |
} | |
case 'A' : | |
{ | |
resultlines[0].Append("----+"); | |
if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append(" o |"); | |
else | |
resultlines[1].Append(" |"); | |
resultlines[2].Append(" |"); | |
break; | |
} | |
case 'B' : | |
{ | |
resultlines[0].Append("+---+"); | |
if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append("| o |"); | |
else | |
resultlines[1].Append("| |"); | |
resultlines[2].Append("| |"); | |
break; | |
} | |
case 'C' : | |
{ | |
resultlines[0].Append("-----"); | |
if (door==j) | |
resultlines[1].Append(" | | "); | |
else if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append(" o "); | |
else | |
resultlines[1].Append(" "); | |
resultlines[2].Append("-----"); | |
break; | |
} | |
case 'D' : | |
{ | |
resultlines[0].Append("+----"); | |
if (door==j) | |
resultlines[1].Append("|| | "); | |
else if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append("| o "); | |
else | |
resultlines[1].Append("| "); | |
resultlines[2].Append("+----"); | |
break; | |
} | |
case 'E' : | |
{ | |
resultlines[0].Append("----+"); | |
if (door==j) | |
resultlines[1].Append(" | ||"); | |
else if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append(" o |"); | |
else | |
resultlines[1].Append(" |"); | |
resultlines[2].Append("----+"); | |
break; | |
} | |
case 'F' : | |
{ | |
resultlines[0].Append("+---+"); | |
if (door==j) | |
resultlines[1].Append("|| ||"); | |
else if(InOut[i][j]=='I' && random.Next(2)==1) | |
resultlines[1].Append("| o |"); | |
else | |
resultlines[1].Append("| |"); | |
resultlines[2].Append("+---+"); | |
break; | |
} | |
default: | |
{ | |
break; | |
} | |
} | |
} | |
foreach(StringBuilder resultline in resultlines) | |
result.Append(resultline+Environment.NewLine); | |
} | |
result.Length--; | |
return result.ToString(); | |
} | |
protected string CloseRoofs(string input) | |
{ | |
StringBuilder result=new StringBuilder(); | |
List<string> lines = new List<string>(input.Split(new char[]{'\n'})); | |
int numlines=lines.Count; | |
int maxheight=numlines; | |
for(int i=0;i<numlines;i++) | |
{ | |
for (int j=0;j<lines[i].Length;j++) | |
{ | |
if (lines[i][j]=='/') | |
{ | |
int cierra=lines[i].IndexOf('\\',j); | |
int requierelineas=(cierra-j+1)/2; | |
while ((numlines-i+requierelineas)>maxheight) | |
{ | |
lines.Insert(0,new string(' ',lines[i].Length)); | |
maxheight++; | |
} | |
} | |
} | |
} | |
for(int i=lines.Count-1;i>0;i--) | |
{ | |
char[] resultarray=lines[i-1].ToCharArray(); | |
for(int j=0;j<lines[i].Length-1;j++) | |
{ | |
if (lines[i][j]=='/') | |
{ | |
if (lines[i][j+2]=='\\'||lines[i][j+1]=='\\') | |
resultarray[j+1]='A'; | |
else | |
resultarray[j+1]='/'; | |
} | |
else if (lines[i][j]=='\\'&&resultarray[j-1]==' '&&resultarray[j]==' ') | |
resultarray[j-1]='\\'; | |
} | |
lines[i-1]=new string(resultarray); | |
} | |
foreach(string line in lines) | |
result.Append(line+Environment.NewLine); | |
result.Length--; | |
return result.ToString(); | |
} | |
public override void Process() | |
{ | |
while (true) | |
{ | |
testcase = testcases.Next(); | |
if (testcase.ToUpper()=="EXIT") | |
break; | |
try | |
{ | |
System.Console.WriteLine(testcase); | |
System.Console.WriteLine("--"); | |
string t=HexOutline(testcase); | |
System.Console.WriteLine(t); | |
// System.Console.WriteLine("--"); | |
// foreach(string s in InOut) | |
// System.Console.WriteLine(s); | |
// System.Console.WriteLine("--"); | |
string a=ASCIIOutline(t); | |
// System.Console.WriteLine(a); | |
// System.Console.WriteLine("--"); | |
string f=CloseRoofs(a); | |
System.Console.WriteLine(f); | |
System.Console.WriteLine("-----"); | |
} | |
catch (Exception e) | |
{ | |
System.Console.WriteLine("Error: {0}",e); | |
} | |
} | |
} | |
} | |
public class Program | |
{ | |
public static void Main (string[] args) | |
{ | |
Tester test = new ASCIIArchitect(args); | |
TimeSpan totaltime = new TimeSpan(0); | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
test.Setup(); | |
sw.Stop(); | |
totaltime = totaltime.Add(sw.Elapsed); | |
System.Console.WriteLine("Setup Time: {0}",sw.Elapsed); | |
sw.Restart(); | |
test.Process(); | |
sw.Stop(); | |
totaltime = totaltime.Add(sw.Elapsed); | |
System.Console.WriteLine("Processing time: {0}",sw.Elapsed); | |
System.Console.WriteLine("Total time: {0}",totaltime); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment