Esempio n. 1
0
        private int Solver(string inData, bool part2)
        {
            //inData = "###########\r\n#0.1.....2#\r\n#.#######.#\r\n#4.......3#\r\n###########\r\n";

            minRouteVal = int.MaxValue;
            //savedRoutes.Clear();
            var                nodeMax   = int.Parse(inData.Where(a => char.IsDigit(a)).Max().ToString());
            string             nodeList  = string.Join("", Enumerable.Range(1, nodeMax).Select(x => x).OrderBy(x => x));
            TileDay24BestRoute startNode = new TileDay24BestRoute(0, 0, nodeList, null);

            return(BestRouteCalculate(inData.Split("\r\n").ToList(), startNode, part2));
        }
Esempio n. 2
0
        private List <TileDay24BestRoute> BestRouteGetNextMoves(List <string> map, TileDay24BestRoute current)
        {
            List <TileDay24BestRoute> moves = new List <TileDay24BestRoute>();

            foreach (var item in current.permitted)
            {
                int    target    = int.Parse(item.ToString());
                string permitted = current.permitted.ReplaceFirst(item.ToString(), "");
                int    route     = CalcRouteTryStored(map, current.id, target);
                var    tmp       = new TileDay24BestRoute(target, route, permitted, current);
                moves.Add(tmp);
            }
            return(moves.OrderBy(x => x.distance).ToList());
        }
Esempio n. 3
0
        public TileDay24BestRoute(int i, int d, string p, TileDay24BestRoute p2)
        {
            distance = d;
            history  = "";
            if (p2 != null)
            {
                distanceTot = distance + p2.distanceTot;
                history     = p2.history + p2.id;
            }
            id        = i;
            permitted = p;
            parent    = p2;
            value     = distance + permitted.Length;
            if (id == 0)
            {
                value = 1000;
            }

            //var tmp = $"{i} {p2.id} {distance} {distanceTot}";
        }
Esempio n. 4
0
        private int BestRouteCalculate(List <string> map, TileDay24BestRoute node, bool part2)
        {
            int park = node.distanceTot + (part2? CalcRouteTryStored(map, node.id, 0) : 0);

            if (park > minRouteVal)
            {
                return(0);
            }

            if (node.permitted.Length == 0)
            {
                if (park < minRouteVal)
                {
                    minRouteVal = park;
                }
            }

            foreach (var move in BestRouteGetNextMoves(map, node))
            {
                BestRouteCalculate(map, move, part2);
            }

            return(minRouteVal);
        }