예제 #1
0
        static void Main()
        {
            string[]      input = File.ReadAllLines("2015\\Day13\\Input\\input.txt");
            List <Person> table = new List <Person>();

            foreach (string arrangement in input)
            {
                string[] parts     = arrangement.Split(' ');
                string   name      = parts[0];
                int      happiness = Int32.Parse(parts[3]);
                string   neighbour = parts[10].Substring(0, parts[10].Length - 1);
                bool     positive  = parts[2] == "gain" ? true : false;
                happiness = positive ? happiness : -1 * happiness;
                if (table.Exists(p => p.name == name))
                {
                    Person person = table.Find(p => p.name == name);
                    person.AddNeighbour(neighbour, happiness);
                }
                else
                {
                    Person person = new Person(name);
                    person.AddNeighbour(neighbour, happiness);
                    table.Add(person);
                }
            }

            Person me = new Person("me");

            foreach (Person person in table)
            {
                person.AddNeighbour("me", 0);
                me.AddNeighbour(person.name, 0);
            }
            table.Add(me);

            Utils.Graphs.Algorithms algs = new Utils.Graphs.Algorithms();
            var permutations             = algs.Permutate <Person>(table, table.Count);
            int minHappiness             = Int32.MaxValue;
            int maxHappiness             = Int32.MinValue;

            foreach (var permutation in permutations)
            {
                int happiness = 0;
                for (int i = 0; i < permutation.Count(); i++)
                {
                    Person person = permutation.ElementAt(i);
                    if (i == 0)
                    {
                        happiness += person.neighbours[permutation.ElementAt(i + 1).name];
                        happiness += person.neighbours[permutation.ElementAt(permutation.Count() - 1).name];
                    }
                    else if (i > 0 && i < permutation.Count() - 1)
                    {
                        happiness += person.neighbours[permutation.ElementAt(i - 1).name];
                        happiness += person.neighbours[permutation.ElementAt(i + 1).name];
                    }
                    else
                    {
                        happiness += person.neighbours[permutation.ElementAt(i - 1).name];
                        happiness += person.neighbours[permutation.ElementAt(0).name];
                    }
                    string padd = i != permutation.Count() - 1 ? " -> " : "";
                    Console.Write(person.name + padd);
                }
                if (happiness < minHappiness)
                {
                    minHappiness = happiness;
                }
                if (happiness > maxHappiness)
                {
                    maxHappiness = happiness;
                }
                Console.Write(" (" + happiness + ")");
                Console.WriteLine();
            }

            Console.WriteLine("Minimum distance = " + minHappiness);
            Console.WriteLine("Maximum distance = " + maxHappiness);
            Console.ReadLine();
        }
예제 #2
0
        static void Main()
        {
            string[]    input  = File.ReadAllLines("2015\\Day09\\Input\\input.txt");
            List <City> cities = new List <City>();

            foreach (string route in input)
            {
                string[] parts    = route.Split(' ');
                int      distance = Int32.Parse(parts[4]);
                City     city1    = new City(parts[0]);
                City     city2    = new City(parts[2]);

                city1.AddNeighbour(city2, distance);
                city2.AddNeighbour(city1, distance);

                if (!cities.Exists(c => c.name == city1.name))
                {
                    cities.Add(city1);
                }
                else
                {
                    City city = cities.First(c => c.name == city1.name);
                    city.AddNeighbour(city2, distance);
                }

                if (!cities.Exists(c => c.name == city2.name))
                {
                    cities.Add(city2);
                }
                else
                {
                    City city = cities.First(c => c.name == city2.name);
                    city.AddNeighbour(city1, distance);
                }
            }
            AdventOfCode.Utils.Graphs.Algorithms algs = new Utils.Graphs.Algorithms();
            var permutations = algs.Permutate <City>(cities, cities.Count);
            int minDistance  = Int32.MaxValue;
            int maxDistance  = Int32.MinValue;

            foreach (var permutation in permutations)
            {
                int distance = 0;
                for (int i = 0; i < permutation.Count(); i++)
                {
                    City city = permutation.ElementAt(i);
                    if (i != permutation.Count() - 1)
                    {
                        distance += city.distances[permutation.ElementAt(i + 1).name];
                    }
                    string padd = i != permutation.Count() - 1 ? " -> " : "";
                    Console.Write(city.name + padd);
                }
                if (distance < minDistance)
                {
                    minDistance = distance;
                }
                if (distance > maxDistance)
                {
                    maxDistance = distance;
                }
                Console.Write(" (" + distance + ")");
                Console.WriteLine();
            }

            Console.WriteLine("Minimum distance = " + minDistance);
            Console.WriteLine("Maximum distance = " + maxDistance);
            Console.ReadLine();
        }