Created
February 15, 2013 15:25
-
-
Save rofr/4961036 to your computer and use it in GitHub Desktop.
And here's using C# for a matrix based (not hexagonal) regex crossword puzzle
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
[Serializable] | |
public class Puzzle | |
{ | |
public string Author{ get; set; } | |
public DateTime Created{ get; set; } | |
public string[] Rows{ get; set; } | |
public string[] Cols { get; set; } | |
public String[] RowConstraints { get; set; } | |
public String[] ColumnConstraints { get; set; } | |
public static Puzzle Create(string[] rows, string[] constraints) | |
{ | |
var puzzle = Create(rows); | |
int size = rows.Length*2; | |
if (constraints.Length != size) | |
{ | |
throw new InvalidDataException("Expected " + size + " regular expressions, got " + constraints.Length); | |
} | |
puzzle.RowConstraints = constraints.Take(rows.Length).ToArray(); | |
puzzle.ColumnConstraints = constraints.Skip(rows.Length).Take(rows.Length).ToArray(); | |
int[] badRows; | |
int[] badCols; | |
if (!puzzle.IsValid(out badRows, out badCols)) | |
{ | |
throw new InvalidDataException("Invalid constraints"); | |
} | |
return puzzle; | |
} | |
public bool IsValid() | |
{ | |
int[] invalidRowIndices; | |
int[] invalidColumnIndices; | |
return IsValid(out invalidRowIndices, out invalidColumnIndices); | |
} | |
public bool IsValid (out int[] invalidRowIndices, out int[] invalidColumnIndices) | |
{ | |
var invalidRows = new List<int>(); | |
var invalidCols = new List<int>(); | |
for (int i = 0; i < Rows.Length; i++) | |
{ | |
if (!IsMatch(RowConstraints[i],Rows[i])) invalidRows.Add(i); | |
if (!IsMatch(ColumnConstraints[i],Cols[i])) invalidCols.Add(i); | |
} | |
invalidColumnIndices = invalidCols.ToArray(); | |
invalidRowIndices = invalidRows.ToArray(); | |
return invalidColumnIndices.Length + invalidRowIndices.Length == 0; | |
} | |
private static bool IsMatch(string unanchoredRegex, string input) | |
{ | |
return Regex.IsMatch(input, "^" + unanchoredRegex + "$"); | |
} | |
public static Puzzle Create(string[] rows) | |
{ | |
int size = rows.Length; | |
int line = 0; | |
foreach(string row in rows.Select(r => r.Trim().ToUpperInvariant())) | |
{ | |
line++; | |
if (row.Length != size || Regex.IsMatch(row, "[^A-Z]")) | |
{ | |
throw new InvalidDataException("Invalid solution on line " + line); | |
} | |
} | |
var puzzle = new Puzzle(); | |
puzzle.Rows = rows; | |
puzzle.Cols = RowsToCols(rows); | |
return puzzle; | |
} | |
private static string[] RowsToCols(string[] rows) | |
{ | |
string[] cols = new string[rows.Length]; | |
for(int i = 0; i< rows.Length;i++) | |
{ | |
int col = i; | |
cols[i] = String.Join("", rows.Select(r => r[col])); | |
} | |
return cols; | |
} | |
} |
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
[TestMethod()] | |
public void CreateTest() | |
{ | |
string[] rows = new string[] | |
{ | |
"ABCDE", | |
"ABCDE", | |
"ABCDE", | |
"ABCDE", | |
"ABCDE" | |
}; | |
string[] constraints = new string[] | |
{ | |
"ABCDE", | |
"ABCDE", | |
"ABCDE", | |
"ABCDE", | |
"ABCDE", | |
"AAAAA", | |
"BBBBB", | |
"CCCCC", | |
"DDDDD", | |
"EEEEE" | |
}; | |
Puzzle target = Puzzle.Create(rows, constraints); | |
Assert.AreEqual(target.Cols.Length, target.Rows.Length); | |
Assert.IsTrue(target.IsValid()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment