Example #1
0
        static guard findGuard(List <guard> guards, int guardId)
        {
            foreach (guard gua in guards)
            {
                if (gua.ID == guardId)
                {
                    return(gua);
                }
            }
            guard g = new guard(guardId);

            guards.Add(g);
            return(g);
        }
Example #2
0
        static void Main()
        {
            string[]     read      = System.IO.File.ReadAllLines(@"E:\Bibliotek\Prog\AdventOfCode\Dec4\input.txt");
            string[]     inputs    = doTheSort(read);
            int          guardId   = 0;
            bool         asleep    = false;
            List <guard> guards    = new List <guard>();
            guard        sleeper   = new guard(0);
            string       oldDate   = "";
            int          startTime = 0;
            int          stopTime  = 0;

            foreach (string input in inputs)
            {
                string date = input.Substring(1, 10);
                if (!date.Equals(oldDate))
                {
                    if (asleep)
                    {
                        sleeper.addSleep(new sleep(oldDate, startTime, 59));
                        asleep = false;
                    }
                    startTime = 0;
                    oldDate   = date;
                }
                int stopPos;
                switch (input[19])
                {
                case 'G':
                    stopPos = getStopPos(input);
                    guardId = Int32.Parse(input.Substring(26, stopPos - 26));
                    sleeper = findGuard(guards, guardId);
                    break;

                case 'w':
                    stopTime = getTime(input);
                    sleeper.addSleep(new sleep(date, startTime, stopTime));
                    asleep = false;
                    break;

                case 'f':
                    startTime = getTime(input);
                    asleep    = true;
                    break;
                }
            }
            foreach (guard g in guards)
            {
                g.calcTotalSleepTime();
            }


            foreach (guard g in guards)
            {
                if (g.totalSleep > sleeper.totalSleep)
                {
                    sleeper = g;
                }
            }
            Console.Write("Guard: ");
            Console.WriteLine(sleeper.ID);
            int sleepmin = sleeper.calcSleepiestTime();

            Console.Write("Sleepiest minute: ");
            Console.WriteLine(sleepmin);
            Console.Write("svar1: ");
            Console.WriteLine(sleeper.ID * sleepmin);
            Console.WriteLine();

            /*foreach (guard g in guards)
             * {
             *  g.timeAsleepOnSleepiest();
             * }
             *
             *
             * foreach (guard g in guards)
             * {
             *  if (g.sleepiestTime[0] > sleeper.sleepiestTime[0])
             *  {
             *      sleeper = g;
             *
             *  }
             * }
             * Console.WriteLine();
             * Console.WriteLine();
             * Console.Write("Guard: ");
             * Console.WriteLine(sleeper.ID);
             * Console.Write("most slept min: ");
             * Console.WriteLine(sleeper.sleepiestTime[0]);
             * Console.Write("svar2: ");
             * Console.Write(sleeper.ID * sleeper.sleepiestTime[0]);
             *
             */

            doTaskB(guards);
        }