fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Diagnostics;
  6.  
  7. namespace ConsoleApp1 {
  8. class Program {
  9. private abstract class Board {
  10. public enum Direction {
  11. Left,
  12. Right,
  13. Up,
  14. Down,
  15. DownLeft,
  16. UpLeft
  17. }
  18. protected String String;
  19. public class Cell {
  20. public HashSet<Char> Set;
  21. public Char Current;
  22. public Cell(String String) {
  23. Set = new HashSet<Char>(String.ToArray());
  24. Current = '\0';
  25. }
  26. }
  27. public class Location {
  28. public Int32 Row;
  29. public Int32 Column;
  30. public Location(Int32 Row, Int32 Column) {
  31. this.Row = Row;
  32. this.Column = Column;
  33. }
  34. public Location(Location From) {
  35. Row = From.Row;
  36. Column = From.Column;
  37. }
  38. }
  39. protected Cell[][] Cells;
  40. public abstract Location GetLocation(Direction Direction, Int32 Distance, Location Locaton);
  41. protected abstract void MakeCells(Object Parameter);
  42. public Board(Object Parameter, String String) {
  43. this.String = String;
  44. MakeCells(Parameter);
  45. var Characters = String.ToArray();
  46. foreach(var Row in Cells) {
  47. foreach(var Cell in Row) {
  48. Cell.Set = new HashSet<Char>(Characters);
  49. }
  50. }
  51. }
  52. protected Cell[] MakeCells(Int32 Size) {
  53. var Cells = new Cell[Size];
  54. for(var Index = 0; Index < Cells.Length; Index++) {
  55. Cells[Index] = new Cell(String);
  56. }
  57. return Cells;
  58. }
  59. public IEnumerable<string> Strings(Char [] Characters, Condition Condition, int Distance) {
  60. if(Distance < Condition.NumberOfCells) {
  61. foreach(var Character in this[GetLocation(Condition.Direction, Distance, Condition.Start)].Set) {
  62. Characters[Distance] = Character;
  63. foreach(var String in Strings(Characters, Condition, Distance + 1)){
  64. yield return String;
  65. }
  66. }
  67. } else {
  68. yield return string.Join("", Characters);
  69. }
  70. }
  71. private IEnumerable<String> Strings(Condition Condition) {
  72. var Characters = new Char[Condition.NumberOfCells];
  73. for(var Index = 0; Index < Characters.Length; Index++) {
  74. Characters[Index] = '\0';
  75. }
  76. foreach(var String in Strings(Characters, Condition, 0)) {
  77. yield return String;
  78. }
  79. }
  80. public Cell this[Location Location]{
  81. get {
  82. return Cells[Location.Row][Location.Column];
  83. }
  84. }
  85. public long Reduce(Condition[] Conditions) {
  86. var Count = 1L;
  87. foreach(var Condition in Conditions){
  88. var Characters = new Char[Condition.NumberOfCells];
  89. var Sets = new HashSet<Char>[Condition.NumberOfCells];
  90. for(var Index = 0; Index < Characters.Length; Index++) {
  91. Characters[Index] = '\0';
  92. Sets[Index] = new HashSet<char>();
  93. }
  94. var RegularExpression = new Regex($"^{Condition.RegularExpression}$", RegexOptions.Compiled);
  95. foreach(var String in Strings(Characters, Condition, 0)) {
  96. if(RegularExpression.IsMatch(String)){
  97. for(var Index = 0; Index < Characters.Length; Index++) {
  98. Sets[Index].Add(String[Index]);
  99. }
  100. }
  101. }
  102. long Count1 = 1;
  103. for(var Index = 0; Index < Sets.Length; Index++) {
  104. this[GetLocation(Condition.Direction, Index, Condition.Start)].Set = Sets[Index];
  105. Count1 *= Sets[Index].Count;
  106. }
  107. Console.WriteLine($"{Condition.NumberOfCells:D}: {Count1:###,###,##0}: {Condition.RegularExpression}");
  108. Count *= Count1;
  109. }
  110. return Count;
  111. }
  112. private IEnumerable<Char[][]> Resolve(Condition[] Conditions, Int32 ConditionIndex) {
  113. if(ConditionIndex < Conditions.Length) {
  114. var Condition = Conditions[ConditionIndex];
  115. var RegularExpression = new Regex($"^{Condition.RegularExpression}$", RegexOptions.Compiled);
  116. foreach(var String in Strings(Condition)) {
  117. if(RegularExpression.IsMatch(String)){
  118. var SaveSets = new HashSet<Char>[Condition.NumberOfCells];
  119. for(var Index = 0; Index < SaveSets.Length; Index++) {
  120. var Cell = this[GetLocation(Condition.Direction, Index, Condition.Start)];
  121. SaveSets[Index] = Cell.Set;
  122. Cell.Set = new HashSet<char>();
  123. Cell.Set.Add(String[Index]);
  124. }
  125. foreach(var Result in Resolve(Conditions, ConditionIndex + 1)) {
  126. yield return Result;
  127. }
  128. for(var Index = 0; Index < SaveSets.Length; Index++) {
  129. var Cell = this[GetLocation(Condition.Direction, Index, Condition.Start)];
  130. Cell.Set = SaveSets[Index];
  131. }
  132. }
  133. }
  134. } else {
  135. var Cells = new Char[this.Cells.Length][];
  136. for(var Row = 0; Row < this.Cells.Length; Row++) {
  137. Cells[Row] = new Char[this.Cells[Row].Length];
  138. for(var Column = 0; Column < this.Cells[Row].Length; Column++) {
  139. Cells[Row][Column] = this.Cells[Row][Column].Set.Single();
  140. }
  141. }
  142. yield return Cells;
  143. }
  144. }
  145. public IEnumerable<Char[][]> Resolve(Condition[] Conditions) {
  146. foreach(var Result in Resolve(Conditions, 0)){
  147. yield return Result;
  148. }
  149. }
  150. }
  151. private class HoneycombBoard: Board {
  152. private Int32 Size;
  153. public override Location GetLocation(Direction Direction, int Distance, Location From) {
  154. var To = new Location(From);
  155. switch(Direction) {
  156. case Direction.Right:
  157. break;
  158. case Direction.DownLeft:
  159. To.Row += Distance;
  160. break;
  161. case Direction.UpLeft:
  162. To.Row -= Distance;
  163. break;
  164. default:
  165. Debug.Fail($"{nameof(GetLocation)}: Row: {Direction.ToString()}");
  166. break;
  167. }
  168. switch(Direction) {
  169. case Direction.Right:
  170. To.Column += Distance;
  171. break;
  172. case Direction.DownLeft:
  173. if(Size - 1 < To.Row) {
  174. To.Column -= To.Row - (Size - 1);
  175. }
  176. break;
  177. case Direction.UpLeft:
  178. if(To.Row < Size - 1) {
  179. To.Column -= Size - 1 - To.Row;
  180. }
  181. break;
  182. default:
  183. Debug.Fail($"{nameof(GetLocation)}: Column: {Direction.ToString()}");
  184. break;
  185. }
  186. return To;
  187. }
  188. protected override void MakeCells(Object Parameter) {
  189. Size = (Int32)Parameter;
  190. Cells = new Cell[2 * Size - 1][];
  191. for(var Row = 0; Row < Size - 1; Row++) {
  192. Cells[Row] = MakeCells(Size + Row);
  193. Cells[2 * Size - 1 - Row - 1] = MakeCells(Size + Row);
  194. }
  195. Cells[Size - 1] = MakeCells(2 * Size - 1);
  196. }
  197. public HoneycombBoard(Int32 Size, String String): base(Size, String) {
  198. }
  199. }
  200. private class Condition {
  201. public readonly String RegularExpression;
  202. public readonly Int32 NumberOfCells;
  203. public readonly Board.Location Start;
  204. public readonly Board.Direction Direction;
  205. public Condition(
  206. String RegularExpression, Int32 NumberOfCells, Int32 Row, Int32 Column,
  207. Board.Direction Direction
  208. ) {
  209. this.RegularExpression = RegularExpression;
  210. this.NumberOfCells = NumberOfCells;
  211. Start = new Board.Location(Row, Column);
  212. this.Direction = Direction;
  213. }
  214. }
  215. static void Main(string[] args) {
  216. var StartTime = DateTime.Now;
  217. var Board = new HoneycombBoard(5, "ABCDEFGHMNORXYZ");
  218. var Conditions = new Condition[]{
  219. new Condition(@"(X|Y|Z).*\1.*\1", 5, 0, 0, Program.Board.Direction.Right),
  220. new Condition(@"[^XYZ]*[XYZ]*", 6, 1, 0, Program.Board.Direction.Right),
  221. new Condition(@"(.).*(.).*\2.*\1", 7, 2, 0, Program.Board.Direction.Right),
  222. new Condition(@"[^X]*X+[^X]*", 8, 3, 0, Program.Board.Direction.Right),
  223. new Condition(@"([^X]|XXY)*", 9, 4, 0, Program.Board.Direction.Right),
  224. new Condition(@"[ABCD]*YO[EFGH]*", 8, 5, 0, Program.Board.Direction.Right),
  225. new Condition(@"[^RX](R|XX)*", 7, 6, 0, Program.Board.Direction.Right),
  226. new Condition(@"(...?)\1*", 6, 7, 0, Program.Board.Direction.Right),
  227. new Condition(@".*Y.*Z+", 5, 8, 0, Program.Board.Direction.Right),
  228. new Condition(@"[^A]B[^C]D[^E]", 5, 0, 0, Program.Board.Direction.DownLeft),
  229. new Condition(@"[^DB]*DB[^DB]*", 6, 0, 1, Program.Board.Direction.DownLeft),
  230. new Condition(@"[BMX]*", 7, 0, 2, Program.Board.Direction.DownLeft),
  231. new Condition(@"(.)(.).*\1.*\2.*\2\1", 8, 0, 3, Program.Board.Direction.DownLeft),
  232. new Condition(@"(XX|YY|ZZ)*[XYZ]{2}.", 9, 0, 4, Program.Board.Direction.DownLeft),
  233. new Condition(@"Y.*N.*", 8, 1, 5, Program.Board.Direction.DownLeft),
  234. new Condition(@"X*F.*G.*H.*", 7, 2, 6, Program.Board.Direction.DownLeft),
  235. new Condition(@"(HY|RM|FX|EH)*", 6, 3, 7, Program.Board.Direction.DownLeft),
  236. new Condition(@".*RZ.*", 5, 4, 8, Program.Board.Direction.DownLeft),
  237. new Condition(@"[^ABE]*BE[^ABE]*Z.*", 5, 8, 0, Program.Board.Direction.UpLeft),
  238. new Condition(@".*[BD]+", 6, 8, 1, Program.Board.Direction.UpLeft),
  239. new Condition(@"[^A]*A[^A]*", 7, 8, 2, Program.Board.Direction.UpLeft),
  240. new Condition(@".*MM.*", 8, 8, 3, Program.Board.Direction.UpLeft),
  241. new Condition(@"[^XO]*XO[^XO]*OX[^XO]*(.)\1", 9, 8, 4, Program.Board.Direction.UpLeft),
  242. new Condition(@"(.+).*\1", 8, 7, 5, Program.Board.Direction.UpLeft),
  243. new Condition(@".*X.X.X.*", 7, 6, 6, Program.Board.Direction.UpLeft),
  244. new Condition(@"H+(..)\1.*", 6, 5, 7, Program.Board.Direction.UpLeft),
  245. new Condition(@".*(MR|CH|RF)[^X]*", 5, 4, 8, Program.Board.Direction.UpLeft)
  246. };
  247. Array.Sort(
  248. Conditions,
  249. (X, Y) => { return X.NumberOfCells.CompareTo(Y.NumberOfCells); }
  250. );
  251. var PreviouseCount = 1L;
  252. for(;;) {
  253. var Count = Board.Reduce(Conditions);
  254. Console.WriteLine($"---- {PreviouseCount:###,###,##0} ⇒ {Count:###,###,##0} --------------------");
  255. if(PreviouseCount == Count){
  256. break;
  257. }
  258. PreviouseCount = Count;
  259. }
  260. foreach(var Cells in Board.Resolve(Conditions)) {
  261. Console.WriteLine("---------------------------------------");
  262. for(var Row = 0; Row < Cells.Length; Row++) {
  263. Console.WriteLine($"{Row:D}: {String.Join("", Cells[Row])}");
  264. }
  265. }
  266. Console.WriteLine("---------------------------------------");
  267. Console.WriteLine($"Time: {(DateTime.Now - StartTime).TotalSeconds:F}");
  268. }
  269. }
  270. }
Time limit exceeded #stdin #stdout 5s 27292KB
stdin
Standard input is empty
stdout
5: 30,375: (X|Y|Z).*\1.*\1
5: 573,300: [^ABE]*BE[^ABE]*Z.*
5: 759,375: .*RZ.*
5: 588: [^A]B[^C]D[^E]
5: 43,875: .*Y.*Z+
5: 88,200: .*(MR|CH|RF)[^X]*