Skip to content

Instantly share code, notes, and snippets.

@ClaudiuGeorgiu
Created August 28, 2015 20:35
Show Gist options
  • Save ClaudiuGeorgiu/de1da85e5e8213cf9a20 to your computer and use it in GitHub Desktop.
Save ClaudiuGeorgiu/de1da85e5e8213cf9a20 to your computer and use it in GitHub Desktop.
Parsing ASCII art numbers with c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ASCIIArtParsingExample
{
class Program
{
static void Main()
{
string ASCIIString = @"
--- --- | | | -----
/ _| | |__| |___
\ | | | |
-- --- | | ____| ";
string[] lines =
ASCIIString.Split(new[] {"\n","\r\n"}, StringSplitOptions.RemoveEmptyEntries);
lines = lines.ReplaceSpacesWithSeparator("$");
ASCIINumbersParser parser = new ASCIINumbersParser(lines, "$");
// Try to find all numbers contained in the ASCII string
foreach (string[] candidate in parser.CandidatesList)
{
for (int i = 1; i < 10; ++i)
{
string[] num = ASCIINumberHelper.GetASCIIRepresentationForNumber(i);
if (ASCIINumberHelper.ASCIIRepresentationMatch(num, candidate))
Console.WriteLine("Number {0} was found in the string.", i);
}
}
}
// Expected output:
// Number 3 was found in the string.
// Number 2 was found in the string.
// Number 1 was found in the string.
// Number 4 was found in the string.
// Number 5 was found in the string.
}
public static class StringHelperClass
{
// Extension method to remove any unnecessary white-space and put a separator char instead.
public static string[] ReplaceSpacesWithSeparator(this string[] text, string separator)
{
// Create an array of StringBuilder, one for every line in the text.
StringBuilder[] stringBuilders = new StringBuilder[text.Length];
// Initialize stringBuilders.
for (int n = 0; n < text.Length; n++)
stringBuilders[n] = new StringBuilder().Append(separator);
// Get shortest line in the text, in order to avoid Out Of Range Exception.
int shorterstLine = text.Min(line => line.Length);
// Temporary variables.
int lastSeparatorIndex = 0;
bool previousCharWasSpace = false;
// Start processing the text, char after char.
for (int n = 0; n < shorterstLine; ++n)
{
// Look for white-spaces on the same position on
// all the lines of the text.
if (text.All(line => line[n] == ' '))
{
// Go to next char if previous char was also a white-space,
// or if this is the first white-space char of the text.
if (previousCharWasSpace || n == 0)
{
previousCharWasSpace = true;
lastSeparatorIndex = n + 1;
continue;
}
previousCharWasSpace = true;
// Append non white-space chars to the StringBuilder
// of each line, for later use.
for (int i = lastSeparatorIndex; i < n; ++i)
{
for (int j = 0; j < text.Length; j++)
stringBuilders[j].Append(text[j][i]);
}
// Append separator char.
for (int j = 0; j < text.Length; j++)
stringBuilders[j].Append(separator);
lastSeparatorIndex = n + 1;
}
else
previousCharWasSpace = false;
}
for (int j = 0; j < text.Length; j++)
text[j] = stringBuilders[j].ToString();
// Return formatted text.
return text;
}
}
public static class ASCIINumberHelper
{
// Get an ASCII art representation of a number.
public static string[] GetASCIIRepresentationForNumber(int number)
{
switch (number)
{
case 1:
return new[]
{
"|",
"|",
"|",
"|"
};
case 2:
return new[]
{
"---",
" _|",
"| ",
"---"
};
case 3:
return new[]
{
"---",
" / ",
@" \ ",
"-- "
};
case 4:
return new[]
{
"| |",
"|__|",
" |",
" |"
};
case 5:
return new[]
{
"-----",
"|___ ",
" |",
"____|"
};
default:
return null;
}
}
// See if two numbers represented as ASCII art are equal.
public static bool ASCIIRepresentationMatch(string[] number1, string[] number2)
{
// Return false if the any of the two numbers is null
// or their lenght is different.
// if (number1 == null || number2 == null)
// return false;
// if (number1.Length != number2.Length)
// return false;
if (number1?.Length != number2?.Length)
return false;
try
{
for (int n = 0; n < number1.Length; ++n)
{
if (number1[n].CompareTo(number2[n]) != 0)
return false;
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex);
return false;
}
return true;
}
}
public class ASCIINumbersParser
{
// Will store a list of all the possible numbers
// found in the text.
public List<string[]> CandidatesList { get; }
public ASCIINumbersParser(string[] text, string separator)
{
CandidatesList = new List<string[]>();
string[][] candidates = new string[text.Length][];
for (int n = 0; n < text.Length; ++n)
{
// Split each line in the text, using the separator char/string.
candidates[n] =
text[n].Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
}
// Put the strings in such a way that each CandidateList item
// contains only one possible number found in the text.
for (int i = 0; i < candidates[0].Length; ++i)
CandidatesList.Add(candidates.Select(c => c[i]).ToArray());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment