Exemplo n.º 1
0
        internal static void part2()
        {
            Queue <coords>           toVisit       = new Queue <coords>();
            Dictionary <coords, int> visitedCoords = new Dictionary <coords, int>();
            List <coords>            visited       = new List <coords>();

            visitedCoords.Add(new coords(1, 1), 0);
            toVisit.Enqueue(new coords(1, 1));
            while (toVisit.Count > 0)
            {
                coords c = toVisit.Dequeue();

                foreach (coords d in adjecentSquares(c))
                {
                    if (visited.Contains(d))
                    {
                        continue;
                    }
                    toVisit.Enqueue(d);
                    visited.Add(d);
                    if (visitedCoords.ContainsKey(c) && !visitedCoords.ContainsKey(d) && visitedCoords[c] < 50)
                    {
                        visitedCoords.Add(d, visitedCoords[c] + 1);
                    }
                }
            }
            Console.WriteLine(visitedCoords.Count(x => x.Value < 51));
        }
Exemplo n.º 2
0
        internal static void part1()
        {
            Queue <coords>           toVisit       = new Queue <coords>();
            Dictionary <coords, int> visitedCoords = new Dictionary <coords, int>();

            visitedCoords.Add(new coords(1, 1), 0);
            toVisit.Enqueue(new coords(1, 1));
            while (toVisit.Count > 0)
            {
                coords c = toVisit.Dequeue();
                if (c.x == targetx && c.y == targety)
                {
                    Console.WriteLine(visitedCoords[c]);
                    return;
                }
                foreach (coords d in adjecentSquares(c))
                {
                    if (visitedCoords.ContainsKey(d))
                    {
                        continue;
                    }
                    toVisit.Enqueue(d);
                    visitedCoords.Add(d, visitedCoords[c] + 1);
                }
            }
        }
Exemplo n.º 3
0
        internal static void part1()
        {
            //input = "ihgpwlah";
            coords current = new coords(0, 0);
            string path    = string.Empty;

            move(path, current);
            Console.WriteLine(shortestpath.Length);
        }
Exemplo n.º 4
0
        private static void move(string path, coords current)
        {
            /*if(path.Length > shortest) {
             *      return;
             * }
             *
             * if(current.x == 3 && current.y == 3) {
             *      if (path.Length <= shortest) {
             *              Console.WriteLine(path);
             *              shortest = path.Length;
             *              shortestpath = path;
             *      }
             *      return;
             * }*/
            if (current.x == 3 && current.y == 3)
            {
                if (path.Length >= shortest)
                {
                    Console.WriteLine(path.Length);
                    shortest     = path.Length;
                    shortestpath = path;
                }
                return;
            }
            string hash = md5hash(input + path).Substring(0, 4);

            if (isOpen(hash[0]) && current.y > 0)
            {
                move(path + "U", new coords(current.x, current.y - 1));
            }
            if (isOpen(hash[1]) && current.y < 3)
            {
                move(path + "D", new coords(current.x, current.y + 1));
            }
            if (isOpen(hash[2]) && current.x > 0)
            {
                move(path + "L", new coords(current.x - 1, current.y));
            }
            if (isOpen(hash[3]) && current.x < 3)
            {
                move(path + "R", new coords(current.x + 1, current.y));
            }
        }
Exemplo n.º 5
0
        internal static List <coords> adjecentSquares(coords c)
        {
            List <coords> adjSquares = new List <coords>();

            if (isOpenSpace(c.x + 1, c.y))
            {
                adjSquares.Add(new coords(c.x + 1, c.y));
            }
            if (isOpenSpace(c.x - 1, c.y))
            {
                adjSquares.Add(new coords(c.x - 1, c.y));
            }
            if (isOpenSpace(c.x, c.y + 1))
            {
                adjSquares.Add(new coords(c.x, c.y + 1));
            }
            if (isOpenSpace(c.x, c.y - 1))
            {
                adjSquares.Add(new coords(c.x, c.y - 1));
            }
            return(adjSquares);
        }