Example #1
0
        static void Main(string[] args)
        {
            var input = "586439172";

            var map          = new Dictionary <int, Cup>();
            var chars        = input.ToCharArray();
            var referenceCup = new Cup {
                value = int.Parse(chars[0].ToString())
            };
            var start = referenceCup;

            map.Add(referenceCup.value, referenceCup);

            //var current = start;
            Cup oneCup = null;

            for (int i = 1; i < chars.Length; i++)
            {
                var cup = new Cup {
                    value = int.Parse(chars[i].ToString())
                };
                map.Add(cup.value, cup);

                if (cup.value == 1)
                {
                    oneCup = cup;
                }
                referenceCup.NextCup = cup;
                referenceCup         = cup;
            }

            var value = 10;

            for (int i = 0; i < 1000000 - input.Length; i++)
            {
                var cup = new Cup {
                    value = value
                };
                map.Add(cup.value, cup);
                referenceCup.NextCup = cup;
                referenceCup         = cup;
                value++;
            }
            referenceCup.NextCup = start;
            referenceCup         = start;

            for (int i = 0; i < 10000000; i++)
            {
                var removed    = referenceCup.NextCup;
                var removedEnd = removed.NextCup.NextCup;

                var removedValues = new HashSet <int>
                {
                    removed.value,
                    removed.NextCup.value,
                    removed.NextCup.NextCup.value
                };
                referenceCup.NextCup = removedEnd.NextCup;

                var refValue = referenceCup.value;
                do
                {
                    refValue--;
                    refValue = refValue == 0 ? 1000000 : refValue;
                }while (removedValues.Contains(refValue));

                var destination = map[refValue];

                var tempNext = destination.NextCup;
                destination.NextCup = removed;
                removedEnd.NextCup  = tempNext;

                referenceCup = referenceCup.NextCup;
            }

            Console.WriteLine(Convert.ToInt64(oneCup.NextCup.value) * Convert.ToInt64(oneCup.NextCup.NextCup.value));
            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            var line = "586439172".ToCharArray();

            Cup referenceCup = null;

            int cupsAdded    = 0;
            Cup lastAddedCup = null;
            Cup newCup;

            for (int i = 0; i < line.Length; i++)
            {
                newCup = new Cup()
                {
                    value = Convert.ToInt32(line[i].ToString())
                };

                if (i == 0)
                {
                    referenceCup = newCup;
                }
                else
                {
                    lastAddedCup.NextCup = newCup;
                }

                lastAddedCup = newCup;
                cupsAdded++;
            }

            for (int i = 10; i <= 1000000; i++)
            {
                newCup = new Cup()
                {
                    value = Convert.ToInt32(line[i].ToString())
                };
                lastAddedCup = newCup;
            }
            lastAddedCup.NextCup = referenceCup;


            for (int i = 0; i <= 10000000; i++)
            {
                var n = referenceCup;
                //for (int j = 0; j < 9; j++)
                //{
                //    Console.Write($"{n.value} ");
                //    n = n.NextCup;
                //}
                //Console.WriteLine();

                // reference to removed cups
                Cup removed = referenceCup.NextCup;

                // reconnect reference cup
                referenceCup.NextCup = referenceCup.NextCup.NextCup.NextCup.NextCup;

                // find destination for removed.
                // find highest number of all lower than reference cup
                List <Tuple <int, Cup> > candidates = new List <Tuple <int, Cup> >();
                n = referenceCup.NextCup;
                for (int j = 0; j < 6; j++)
                {
                    if (n.value < referenceCup.value)
                    {
                        candidates.Add(new Tuple <int, Cup>(n.value, n));
                    }
                    n = n.NextCup;
                }

                Cup destination;

                if (candidates.Count == 0)
                {
                    n = referenceCup.NextCup;
                    for (int j = 0; j < 6; j++)
                    {
                        candidates.Add(new Tuple <int, Cup>(n.value, n));
                        n = n.NextCup;
                    }
                    destination = candidates.First(g => g.Item1 == candidates.Max(f => f.Item1)).Item2;
                }
                else
                {
                    destination = candidates.First(g => g.Item1 == candidates.Max(f => f.Item1)).Item2;
                }

                Console.WriteLine($"Reconnecting to {destination}");

                // Reconnect at destination
                var nextatdest = destination.NextCup;
                destination.NextCup             = removed;
                removed.NextCup.NextCup.NextCup = nextatdest;

                referenceCup = referenceCup.NextCup;

                if (i % 1000 == 0)
                {
                    Console.WriteLine(i);
                }
            }
        }