using System.Collections.Generic;
public bool useTestData = false;
Console.WriteLine("Tests starting");
Console.WriteLine("Step 1 Starting");
Console.WriteLine("Found ZZZ in {0} steps.", FindZ());
Console.WriteLine("Step 2 Starting");
Console.WriteLine("{0} starting positions found", _input.Step2As.Count);
long answer = FindZWithFullPathsAndDistances();
Console.WriteLine("All paths lead to Z in {0} steps.", answer);
string currElement = "AAA";
foreach (var c in _input.Instruction)
currElement = _input.Elements[currElement].Item1;
currElement = _input.Elements[currElement].Item2;
if (currElement == "ZZZ")
throw new Exception("are we infinitely looping?");
public long FindZ(List<string> startingPositions)
List<string> currPos = startingPositions.ToList();
foreach (var c in _input.Instruction)
List<string> newPos = new List<string>();
foreach (var cp in currPos)
thisPos = _input.Elements[cp].Item1;
thisPos = _input.Elements[cp].Item2;
Console.WriteLine("welp... something's up");
if (fullLoop % 10000 == 0)
Console.WriteLine("Current places after {0} loops", fullLoop);
foreach (var cp in currPos)
Console.WriteLine(" {0}", cp);
Console.WriteLine(string.Join("\", \"", currPos));
public long FindZWithFullPaths()
Dictionary<string, ElementPath> fullPaths = GetFullPaths();
var currentElements = _input.Step2As;
currentElements = new List<string>() { "TJC", "RGL", "SBV", "HFG", "LPX", "PQT" };
runningCount = 151740000000;
int commonZPos = FindCommonZ(currentElements, fullPaths);
return runningCount + commonZPos;
List<string> newElements = new List<string>();
foreach (var ce in currentElements)
newElements.Add(fullPaths[ce].EndingElement);
currentElements = newElements;
runningCount += _input.Instruction.Length;
if (loopCount % 5000000 == 0)
Console.WriteLine("Got to {0} loops (total instruction count {1}", loopCount, runningCount);
Console.WriteLine("Current Elements: \"{0}\"", string.Join("\", \"", currentElements));
public int FindCommonZ(List<string> currentElements, Dictionary<string, ElementPath> fullPaths)
List<int> sharedZs = null;
foreach (var ce in currentElements)
sharedZs = fullPaths[ce].ZPositions;
sharedZs = sharedZs.Intersect(fullPaths[ce].ZPositions).ToList();
public long FindZWithFullPathsAndDistances()
Dictionary<string, ElementPath> fullPaths = GetFullPaths();
Dictionary<string, Tuple<long, string>> pathDistances = GetPathDistances(fullPaths);
public Dictionary<string, Tuple<long, string>> GetPathDistances(Dictionary<string, ElementPath> fullPaths)
Dictionary<string, Tuple<long, string>> pathDistances = new Dictionary<string, Tuple<long, string>>();
foreach (var fp in fullPaths)
var nextElem = fp.Value.EndingElement;
while (nextElem[2] != 'Z')
nextElem = fullPaths[nextElem].EndingElement;
pathDistances.Add(key, new Tuple<long, string>(dist, nextElem));
if (key == "CSN" || key[2] == 'A' || key[2] == 'Z')
Console.WriteLine(" Distance for {0} is {1} and ends at {2}", key, dist, nextElem);
public Dictionary<string, ElementPath> GetFullPaths()
Console.WriteLine("Instructions: {0}", _input.Instruction);
DateTime start = DateTime.Now;
Dictionary<string, ElementPath> fullPaths = new Dictionary<string, ElementPath>();
foreach (var e in _input.Elements)
ElementPath ep = new ElementPath();
ep.ZPositions = new List<int>();
ep.StartingElement = e.Key;
string currElement = ep.StartingElement;
foreach (var i in _input.Instruction)
currElement = _input.Elements[currElement].Item1;
currElement = _input.Elements[currElement].Item2;
if (currElement[2] == 'Z')
ep.ZPositions.Add(instructionPos);
ep.EndingElement = currElement;
fullPaths.Add(ep.StartingElement, ep);
if (ep.ZPositions.Count > 0)
Console.WriteLine("FullPath for {0}: [{1}] and ends at {2}", ep.StartingElement, string.Join(",", ep.ZPositions.Select(z=>z.ToString())), ep.EndingElement);
Console.WriteLine("GetFullPaths took: {0}", (DateTime.Now - start));
public string Instruction { get; set; }
public Dictionary<string, Tuple<string, string>> Elements { get; set; }
public List<string> Step2As { get; set; }
public string StartingElement { get; set; }
public List<int> ZPositions { get; set; }
public string EndingElement { get; set; }
string[] raw = GetRaw().Split('\n');
_input.Elements = new Dictionary<string, Tuple<string, string>>();
_input.Step2As = new List<string>();
Console.WriteLine(s.Length);
string key = s.Substring(0, 3);
string l = s.Substring(7, 3);
string r = s.Substring(12, 3);
_input.Elements.Add(key, new Tuple<string, string>(l, r));
if (step == 2 || step == 0)
return @"LRLRLRLLRRLRRLRRRLRRLRLLRRRLRRRLRRLLLLRRRLRLLRRLRRLRRLLLRRRLRRRLRRLRLRRLRLRLRLLRRRLRRRLLRRRLRRRLRRRLRLLLRRLRLRRRLRLRRRLLRRRLRLLRLRRRLRLRRRLRRLLRLRLRRLRLRLRRLRLRLRRRLRRLRLLRRLRRRLRRRLRRLRRRLRRLRLRRRLLRRRLLRRLRLRRRLRRRLLRRRLRLRRLRLRLRRLRLLRRLRLRLRRLRRRLRRRLRLRRLRRLLLRRRLLRLRRRLLRRRR