Example #1
0
        static void Main(string[] args)
        {
            var contents = System.IO.File.ReadAllLines("input.txt");

            List <event_record> records = new List <event_record>();

            int guard_on_duty = 0;

            for (int i = 0; i < contents.Length; i++)
            {
                string currow = contents[i];

                if (currow.Contains("#"))
                {
                    guard_on_duty = Int32.Parse(currow.Split(" ")[3].Replace("#", ""));
                }
                else
                {
                    guard_on_duty = 0;
                }

                event_record record = new event_record()
                {
                    timestamp   = pull_datetime_from_record(currow),
                    index       = i,
                    begin_shift = currow.Contains("#"),
                    fall_asleep = currow.Contains("falls"),
                    wake_up     = currow.Contains("wakes"),
                    guard_id    = guard_on_duty
                };

                records.Add(record);
            }

            int guard_last_seen = 0;

            foreach (var record in records.OrderBy(e => e.timestamp))
            {
                if (record.guard_id != 0)
                {
                    guard_last_seen = record.guard_id;
                }
                else
                {
                    record.guard_id = guard_last_seen;
                }
            }

            Part1(records);
        }
Example #2
0
        public static void Part1(List <event_record> records)
        {
            List <int> guards_list = records.Select(e => e.guard_id).Distinct().ToList();

            List <guard> guards = new List <guard>();

            foreach (var item in guards_list)
            {
                guard guard = new guard();

                guard.id = item;

                List <event_record> specific_list = records.Where(e => e.guard_id == item && e.begin_shift == false).OrderBy(e => e.timestamp).ToList();

                for (int i = 0; i < specific_list.Count(); i = i + 2)
                {
                    event_record currow   = specific_list[i];
                    event_record next_row = specific_list[i + 1];

                    TimeSpan span = next_row.timestamp.Subtract(currow.timestamp);

                    guard.total_sleep += span.Minutes;

                    DateTime start = currow.timestamp;

                    while (start < next_row.timestamp)
                    {
                        guard.asleep_minute.Add(start.Minute);

                        start = start.AddMinutes(1);
                    }
                }

                guards.Add(guard);
            }

            Console.WriteLine("Sleepiest Guard");
            Console.WriteLine(guards.OrderByDescending(e => e.total_sleep).First().id);
            Console.WriteLine("Sleepiest Minute");
            var most = (from i in guards.First(e => e.id == guards.OrderByDescending(f => f.total_sleep).First().id).asleep_minute
                        group i by i into grp
                        orderby grp.Count() descending
                        select grp.Key).First();

            Console.WriteLine(most);
            Console.WriteLine("Multiplied");
            Console.WriteLine(guards.OrderByDescending(e => e.total_sleep).First().id *most);

            Part2(guards);
        }