コード例 #1
0
ファイル: Day4.cs プロジェクト: Ctri-The-Third/AoC18
        public Day4day(DateTime timestamp, ref Day4Guard guard)
        {
            this.timestamp = timestamp;
            this.guard     = guard;

            for (int i = 0; i < 60; i++)
            {
                minutesAwake[i] = true;
            }
        }
コード例 #2
0
ファイル: Day4.cs プロジェクト: Ctri-The-Third/AoC18
        public static string solvePart1()
        {
            Day4Guard maxGuard = new Day4Guard(-1);

            foreach (Day4Guard guard in guards.Values)
            {
                if (guard.totalTimeAsleep > maxGuard.totalTimeAsleep)
                {
                    maxGuard = guard;
                }
            }
            var minutes = new int[60];

            for (int i = 0; i < 60; i++)
            {
                minutes[i] = 0;
            }
            foreach (Day4day day in days)
            {
                if (day.guard.ID == maxGuard.ID) //filter only on the correct guards
                {
                    string dbgout = "[";
                    for (int i = 0; i < 60; i++)
                    {
                        if (day.minutesAwake[i] == false)
                        {
                            minutes[i] += 1;
                        }
                        dbgout += minutes[i] + ",";
                    }
                    dbgout += "]";
                }
            }

            var maxminute = 0;
            var maxID     = 0;

            for (int i = 0; i < 60; i++)
            {
                if (minutes[i] > maxminute)
                {
                    maxminute = minutes[i];
                    maxID     = i;
                }
            }

            var outstring = "Guard ID[" + maxGuard.ID + "] was asleep.";

            outstring += "]<br/> optimal minute = [" + maxID + ", " + maxminute + " times]<br/>";
            outstring += "Checksum = <b>[" + (maxID * maxGuard.ID) + "]</b>";
            return(outstring);
        }
コード例 #3
0
ファイル: Day4.cs プロジェクト: Ctri-The-Third/AoC18
        public static string parse(string problem)
        {
            /* //
             *  [1518-11-01 00:00] Guard #10 begins shift
             *  [1518-11-01 00:05] falls asleep
             *  [1518-11-01 00:25] wakes up
             *  [1518-11-01 00:30] falls asleep
             *  [1518-11-01 00:55] wakes up
             *  [1518-11-01 23:58] Guard #99 begins shift
             *  [1518-11-02 00:40] falls asleep
             *  [1518-11-02 00:50] wakes up
             *  [1518-11-03 00:05] Guard #10 begins shift
             *  [1518-11-03 00:24] falls asleep
             *  [1518-11-03 00:29] wakes up
             *  [1518-11-04 00:02] Guard #99 begins shift
             *  [1518-11-04 00:36] falls asleep
             *  [1518-11-04 00:46] wakes up
             *  [1518-11-05 00:03] Guard #99 begins shift
             *  [1518-11-05 00:45] falls asleep
             *  [1518-11-05 00:55] wakes up
             */

            entries = new List <Day4Entry>();
            var problems = problem.Split("\r\n");

            foreach (string unparsedEntry in problems)
            {
                Day4Entry entry = new Day4Entry(unparsedEntry);
                entries.Add(entry);
            }

            entries.Sort();
            string returnvalue = "";

            foreach (var entry in entries)
            {
                returnvalue += entry.ToString() + "<br/>";
            }

            //now that we're sorted, we can do several things
            //loop through the entries and get days
            //simultaneously get a list of guards

            guards = new Dictionary <int, Day4Guard>();
            days   = new List <Day4day>();
            int guardID;

            var     currentGuard = new Day4Guard(-1);
            Day4day currentDay   = new Day4day(new DateTime(1900, 1, 1), ref currentGuard);



            //iterate through all sorted entries, creating guards as necessary, adding minutes as necessary.


            foreach (var entry in entries)
            {
                //[1518-11-01 23:58] Guard #99 begins shift
                //[1518 - 11 - 02 00:40] falls asleep
                //[1518 - 11 - 02 00:50] wakes up
                if (entry.action.Substring(0, 5) == "Guard")
                {
                    //new shift, create day, create guard.
                    //assign guard to day


                    var IDPieces = entry.action.Split(" ");
                    guardID = int.Parse(IDPieces[1].Substring(1));
                    guards.TryGetValue(guardID, out currentGuard);
                    if (currentGuard == null)
                    {
                        currentGuard = new Day4Guard(guardID);
                        guards.TryAdd(currentGuard.ID, currentGuard);
                    }

                    currentDay = new Day4day(entry.timestamp, ref currentGuard);
                    days.Add(currentDay);
                }
                else if (entry.action.Substring(0, 5) == "falls")
                {
                    //First the guard is set to being asleep
                    //second the timestamp is set
                    currentDay.guard.fallAsleep(entry.timestamp);
                }
                else if (entry.action.Substring(0, 5) == "wakes")
                {
                    //find out the time they fell asleep
                    //find out the time they woke up
                    //add time asleep to day.
                    currentGuard.awaken(entry.timestamp);
                    var startsleep = currentGuard.timeFellAsleep;

                    currentGuard.totalTimeAsleep += Convert.ToInt32((entry.timestamp - startsleep).TotalMinutes);
                    for (int i = startsleep.Minute; i < entry.timestamp.Minute; i++)
                    {
                        currentDay.minutesAwake[i] = false;
                        currentGuard.timeasleepperminute[i]++;
                    }
                }
            }



            return(returnvalue);
        }