public IEnumerable <Day17MazeState> GetNextStates(Day17MazeState state) { if (state.Location.X == 3 && state.Location.Y == 3) { yield break; } //Console.WriteLine(state.Path); var hash = _hasher.Md5Hash(state.Path).ToLower(); if (IsOpen(hash[0]) && state.Location.Y > 0) //Up { yield return(new Day17MazeState(state, CompassBearing.North)); } if (IsOpen(hash[1]) && state.Location.Y < 3) //Down { yield return(new Day17MazeState(state, CompassBearing.South)); } if (IsOpen(hash[2]) && state.Location.X > 0) //Left { yield return(new Day17MazeState(state, CompassBearing.West)); } if (IsOpen(hash[3]) && state.Location.X < 3) //Left { yield return(new Day17MazeState(state, CompassBearing.East)); } }
public string Solve(string passcode) { var startState = new Day17MazeState { Location = new Location(0, 0), Path = passcode }; HashSet <Day17MazeState> toExplore = new HashSet <Day17MazeState> { startState }; List <string> validRoutes = new List <string>(); while (toExplore.Any()) { toExplore = new HashSet <Day17MazeState>(toExplore.SelectMany(GetNextStates)); var success = toExplore.Where(x => x.Location.X == 3 && x.Location.Y == 3); if (success.Any()) { return(success.OrderBy(x => x.Path.Length).First().Path.Remove(0, passcode.Length)); } } return(validRoutes.OrderBy(x => x.Length).First().Remove(0, passcode.Length)); }
public Day17MazeState(Day17MazeState state, CompassBearing direction) { Path = state.Path; Location = new Location(state.Location.X, state.Location.Y); Path = state.Path + GetNextPath(direction); if (direction == CompassBearing.North) { Location.Y--; } else if (direction == CompassBearing.East) { Location.X++; } else if (direction == CompassBearing.South) { Location.Y++; } else if (direction == CompassBearing.West) { Location.X--; } }
protected bool Equals(Day17MazeState other) { return(string.Equals(Path, other.Path) && Equals(Location, other.Location)); }