|
|
@@ -17,88 +17,112 @@ namespace D20._1
|
|
|
var regex = file.ReadToEnd();
|
|
|
file.Close();
|
|
|
|
|
|
+ //regex = @"^E(N|S(E|N))ES$";
|
|
|
regex = @"^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$";
|
|
|
|
|
|
List<int> result = new List<int>();
|
|
|
- Stack<string> queue = new Stack<string>();
|
|
|
+ var str = regex.TrimStart('^').TrimEnd('$');
|
|
|
|
|
|
- queue.Push(regex.TrimStart('^').TrimEnd('$'));
|
|
|
+ var allPath = RunPath(str, result);
|
|
|
+ int max = 0;
|
|
|
+ foreach (var path in allPath)
|
|
|
+ {
|
|
|
+ var r = GetResult(path);
|
|
|
+ if (r > max) max = r;
|
|
|
+ }
|
|
|
|
|
|
- while (queue.Count > 0)
|
|
|
+ Console.WriteLine(max);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static IEnumerable<string> RunPath(string str, List<int> result)
|
|
|
+ {
|
|
|
+ bool hasParenthesis = false;
|
|
|
+ int minLength = 0, minIndex = 0;
|
|
|
+
|
|
|
+ int par = 0, index = 0;
|
|
|
+ List<int> pipes = new List<int>();
|
|
|
+ foreach (var c in str)
|
|
|
{
|
|
|
- bool hasParenthesis = false;
|
|
|
- int minLength = 0, minIndex = 0;
|
|
|
+ if (c == '(') par++;
|
|
|
+ if (c == ')')
|
|
|
+ {
|
|
|
+ par--;
|
|
|
+ if (par == 0) break;
|
|
|
+ }
|
|
|
|
|
|
- var str = queue.Pop();
|
|
|
+ if (par == 1 && c == '|')
|
|
|
+ {
|
|
|
+ pipes.Add(index);
|
|
|
+ }
|
|
|
|
|
|
- int par = 0, index = 0;
|
|
|
- List<int> pipes = new List<int>();
|
|
|
- foreach (var c in str)
|
|
|
+ if (hasParenthesis == false && par == 1)
|
|
|
{
|
|
|
- if (c == '(') par++;
|
|
|
- if (c == ')')
|
|
|
- {
|
|
|
- par--;
|
|
|
- if (par == 0) break;
|
|
|
- }
|
|
|
+ minIndex = index;
|
|
|
+ hasParenthesis = true;
|
|
|
+ }
|
|
|
|
|
|
- if (par == 1 && c == '|')
|
|
|
- {
|
|
|
- pipes.Add(index);
|
|
|
- }
|
|
|
+ if (par != 0) minLength++;
|
|
|
|
|
|
- if (hasParenthesis == false && par == 1)
|
|
|
- {
|
|
|
- minIndex = index;
|
|
|
- hasParenthesis = true;
|
|
|
- }
|
|
|
+ index++;
|
|
|
+ }
|
|
|
|
|
|
- if (par != 0) minLength++;
|
|
|
+ if (pipes.Count == 0)
|
|
|
+ return new[] { str };
|
|
|
|
|
|
- index++;
|
|
|
- }
|
|
|
+ var possibilitiesStr = "";
|
|
|
+ if (hasParenthesis)
|
|
|
+ possibilitiesStr = str.Substring(minIndex + 1, minLength - 1);
|
|
|
+ else
|
|
|
+ possibilitiesStr = str.Substring(minIndex, minLength);
|
|
|
|
|
|
- if (hasParenthesis == false)
|
|
|
- {
|
|
|
- (int x, int y) pos = (0, 0);
|
|
|
- HashSet<((int x, int y) from, (int x, int y) to)> doors = new HashSet<((int x, int y) from, (int x, int y) to)>();
|
|
|
- foreach (var c in str)
|
|
|
- {
|
|
|
- var bpos = pos;
|
|
|
- switch (c)
|
|
|
- {
|
|
|
- case 'N': pos.y -= 1; break;
|
|
|
- case 'S': pos.y += 1; break;
|
|
|
- case 'W': pos.x -= 1; break;
|
|
|
- case 'E': pos.x += 1; break;
|
|
|
- }
|
|
|
-
|
|
|
- doors.Add((bpos, pos));
|
|
|
- doors.Add((pos, bpos));
|
|
|
- }
|
|
|
+ List<string> possibilities = new List<string>();
|
|
|
|
|
|
- result.Add(GetBreadthFirstSearch(doors, pos));
|
|
|
- continue;
|
|
|
- }
|
|
|
+ int beforePipe = minIndex;
|
|
|
+ for (var i = 0; i < pipes.Count; ++i)
|
|
|
+ {
|
|
|
+ possibilities.Add(possibilitiesStr.Substring(beforePipe - minIndex, pipes[i] - beforePipe - 1));
|
|
|
+ beforePipe = pipes[0];
|
|
|
+ }
|
|
|
+ possibilities.Add(possibilitiesStr.Substring(beforePipe - minIndex));
|
|
|
|
|
|
- var possibilitiesStr = str.Substring(minIndex + 1, minLength - 1);
|
|
|
- List<string> possibilities = new List<string>();
|
|
|
+ List<string> results = new List<string>();
|
|
|
+ foreach (var possibility in possibilities)
|
|
|
+ {
|
|
|
+ var rr = RunPath(possibility, result);
|
|
|
|
|
|
- int beforePipe = minIndex;
|
|
|
- for (var i = 0; i < pipes.Count; ++i)
|
|
|
+ foreach (var r in rr)
|
|
|
{
|
|
|
- possibilities.Add(possibilitiesStr.Substring(beforePipe - minIndex, pipes[i] - beforePipe - 1));
|
|
|
- beforePipe = pipes[0];
|
|
|
+ var nr = str.Substring(0, minIndex) + r + str.Substring(minIndex + minLength + 1);
|
|
|
+
|
|
|
+ var rs = RunPath(nr, result);
|
|
|
+ foreach (var s in rs)
|
|
|
+ results.Add(s);
|
|
|
}
|
|
|
- possibilities.Add(possibilitiesStr.Substring(beforePipe - minIndex));
|
|
|
+ }
|
|
|
+
|
|
|
+ return results;
|
|
|
+ }
|
|
|
|
|
|
- foreach (var possibility in possibilities)
|
|
|
+ private static int GetResult(string str)
|
|
|
+ {
|
|
|
+ (int x, int y) pos = (0, 0);
|
|
|
+ HashSet<((int x, int y) from, (int x, int y) to)> doors = new HashSet<((int x, int y) from, (int x, int y) to)>();
|
|
|
+ foreach (var c in str)
|
|
|
+ {
|
|
|
+ var bpos = pos;
|
|
|
+ switch (c)
|
|
|
{
|
|
|
- var nr = str.Substring(0, minIndex) + possibility + str.Substring(minIndex + minLength + 1);
|
|
|
- queue.Push(nr);
|
|
|
+ case 'N': pos.y -= 1; break;
|
|
|
+ case 'S': pos.y += 1; break;
|
|
|
+ case 'W': pos.x -= 1; break;
|
|
|
+ case 'E': pos.x += 1; break;
|
|
|
}
|
|
|
+
|
|
|
+ doors.Add((bpos, pos));
|
|
|
+ doors.Add((pos, bpos));
|
|
|
}
|
|
|
- Console.WriteLine(result.Max());
|
|
|
+
|
|
|
+ return GetBreadthFirstSearch(doors, pos);
|
|
|
}
|
|
|
|
|
|
|