-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathCrosswordPuzzle.cs
139 lines (118 loc) · 4.59 KB
/
CrosswordPuzzle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Crossword Puzzle
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the crosswordPuzzle function below.
static string[] crosswordPuzzle(string[] crossword, string words)
{
var wordsArray = words.Split(';').ToArray();
if (TryFill(crossword.Select(x => x.Replace("-", ".")).ToArray(), wordsArray, out var result))
return result;
else
throw new Exception("Could not find a solution.");
}
static bool TryFill(
string[] crossword,
string[] words,
out string[] result)
{
result = crossword.Select(x => x).ToArray();
var regex = new Regex("[^\\+]*\\.[^\\+]*");
var colCount = crossword[0].Length;
var rowCount = crossword.Length;
var pivotCrossword = new string[colCount];
for (var i = 0; i < rowCount; i++)
for (var j = 0; j < colCount; j++)
pivotCrossword[j] = (pivotCrossword[j] ?? "") + crossword[i][j];
for (var index = 0; index < rowCount; index++)
{
foreach (Match match in regex.Matches(result[index]))
{
var matchRegex = new Regex(match.Value);
foreach (var word in words.Where(x => x.Length == match.Value.Length && matchRegex.IsMatch(x)))
{
var temp = result[index];
for (var j = 0; j < match.Length; j++)
result[index] = Replace(result[index], match.Index + j, word[j]);
if (TryFill(result, words.Where(x => x != word).ToArray(), out var grandResult))
{
result = grandResult;
return true;
}
else
{
result[index] = temp;
}
//for (int j = 0; j < match.Length; j++)
// pivotCrossword[match.Index + j] = Replace(
// pivotCrossword[match.Index + j],
// index + j,
// word[j]);
}
}
foreach (Match match in regex.Matches(pivotCrossword[index]))
{
var matchRegex = new Regex(match.Value);
foreach (var word in words.Where(x => x.Length == match.Value.Length && matchRegex.IsMatch(x)))
{
var temp = "";
for (var j = 0; j < match.Length; j++)
{
temp += match.Value[j];
result[match.Index + j] = Replace(
result[match.Index + j],
index,
word[j]);
}
if (TryFill(result, words.Where(x => x != word).ToArray(), out var grandResult))
{
result = grandResult;
return true;
}
else
{
for (var j = 0; j < match.Length; j++)
{
temp += pivotCrossword[match.Index + j];
result[match.Index + j] = Replace(
result[match.Index + j],
index + j,
temp[j]);
}
}
}
}
}
return !result.Any(x => regex.Matches(x).Count > 0);
}
static string Replace(string input, int index, char @char)
{
var array = input.ToCharArray();
array[index] = @char;
return string.Concat(array);
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] crossword = new string [10];
for (int i = 0; i < 10; i++) {
string crosswordItem = Console.ReadLine();
crossword[i] = crosswordItem;
}
string words = Console.ReadLine();
string[] result = crosswordPuzzle(crossword, words);
textWriter.WriteLine(string.Join("\n", result));
textWriter.Flush();
textWriter.Close();
}
}