using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace D20._1 { class Program { static (int x, int y) MoveTo(char c, (int x, int y) 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; } return pos; } static Dictionary<(int x, int y), int> makeMap(string input) { var result = new Dictionary<(int x, int y), int>(); var nodes = new Stack<((int x, int y), int dist)>(); ((int x, int y) pos, int dist) cur = ((0, 0), 0); foreach (var c in input) { switch (c) { case 'N': case 'S': case 'E': case 'W': cur.pos = MoveTo(c, cur.pos); cur.dist++; result.TryAdd(cur.pos, cur.dist); break; case '(': nodes.Push(cur); break; case '|': cur = nodes.Peek(); break; case ')': cur = nodes.Pop(); break; } } return result; } static void Main(string[] args) { if (args.Length < 1) return; if (File.Exists(args[0]) == false) return; var file = File.OpenText(args[0]); var regex = file.ReadToEnd(); file.Close(); //regex = @"^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$"; //regex = @"^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$"; //regex = @"^N(EEENWWW|N)$"; regex = regex.TrimStart('^').TrimEnd('$'); var map = makeMap(regex); int max = map.Max(p => p.Value); int d = map.Count(p => p.Value >= 1000); Console.WriteLine($"Ex 1 : {max}\nEx 2 ; {d}"); } } }