Equd

AdventOfCode 2018 Day 04

Dec 4th, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. void Main()
  2. {
  3.     //test
  4.     var input = new string[]{
  5. #region
  6.     "[1518-11-01 00:00] Guard #10 begins shift",
  7.     "[1518-11-01 00:05] falls asleep          ",
  8.     "[1518-11-01 00:25] wakes up              ",
  9.     "[1518-11-01 00:30] falls asleep          ",
  10.     "[1518-11-01 00:55] wakes up              ",
  11.     "[1518-11-01 23:58] Guard #99 begins shift",
  12.     "[1518-11-02 00:40] falls asleep          ",
  13.     "[1518-11-02 00:50] wakes up              ",
  14.     "[1518-11-03 00:05] Guard #10 begins shift",
  15.     "[1518-11-03 00:24] falls asleep          ",
  16.     "[1518-11-03 00:29] wakes up              ",
  17.     "[1518-11-04 00:02] Guard #99 begins shift",
  18.     "[1518-11-04 00:36] falls asleep          ",
  19.     "[1518-11-04 00:46] wakes up              ",
  20.     "[1518-11-05 00:03] Guard #99 begins shift",
  21.     "[1518-11-05 00:45] falls asleep          ",
  22.     "[1518-11-05 00:55] wakes up              ",
  23.     #endregion
  24. };
  25.  
  26.     Guard.CreateGuards(input);
  27.  
  28.     Debug.Assert(Guard.AnswerA() == 240);
  29.     Debug.Assert(Guard.AnswerB() == 4455);
  30.  
  31.     //create aoc
  32.     var aoc = new AdventOfCode(2018, 4, Login.Google);
  33.        
  34.     //sort lines
  35.     input = aoc.InputLines;
  36.    
  37.     //create guards
  38.     Guard.CreateGuards(aoc.InputLines);
  39.    
  40.     //get answers
  41.     aoc.SubmitAnswer(Guard.AnswerA(), Part.A);
  42.     aoc.SubmitAnswer(Guard.AnswerB(), Part.B);
  43. }
  44.  
  45.  
  46. class Guard
  47. {  
  48.     private static Guard[] allGuards;
  49.  
  50.     //id of the guards
  51.     public string id;
  52.    
  53.     //track all sleeping minutes
  54.     public int[] sleep = new int[60];
  55.    
  56.    
  57.     public int countMinutesAsleep; 
  58.     public int minuteSleepingTheMost;
  59.    
  60.     public int maxSleepingCount => sleep[minuteSleepingTheMost];
  61.    
  62.     public int[] Parse()
  63.     {                  
  64.         countMinutesAsleep = sleep.Sum(); //count all the minutes asleep
  65.         minuteSleepingTheMost = sleep
  66.             .Select((x, index) => (x, index)) //create index/minute
  67.             .OrderByDescending(y=> y.x).First().index; //sort the minute that is most often asleep
  68.        
  69.         return sleep;
  70.     }  
  71.    
  72.     public static void CreateGuards(string[] lines)
  73.     {
  74.         var guards = new Dictionary<string, Guard>();
  75.  
  76.         Guard activeGuard = null; //current guard
  77.        
  78.         lines = lines.OrderBy(x => x).ToArray();
  79.  
  80.         for(int i = 0; i < lines.Length; i++)
  81.         {
  82.             var match = Regex.Match(lines[i], @"Guard #(\d+)"); //get id
  83.             if (match.Success)
  84.             {
  85.                 //create new guard?
  86.                 if (guards.ContainsKey(match.Groups[1].Value) == false)
  87.                 {
  88.                     activeGuard = new Guard() { id = match.Groups[1].Value };
  89.                    
  90.                     //add to dic
  91.                     guards.Add(activeGuard.id, activeGuard);
  92.                 }
  93.                 //get guard
  94.                 activeGuard = guards[match.Groups[1].Value];
  95.             }
  96.             else if (lines[i].Contains("asleep") && lines[i + 1].Contains("wakes up"))
  97.             {
  98.                 var start = int.Parse(lines[i].Substring(15, 2)); //get start minute
  99.                 var end = int.Parse(lines[i + 1].Substring(15, 2)); //endMinute
  100.  
  101.                 while (start != end)
  102.                 {
  103.                     //increment minute and increment minute counter in sleep
  104.                     activeGuard.sleep[start++]++;
  105.                 }
  106.             }
  107.  
  108.         }
  109.  
  110.         //parse the content for the guards
  111.         guards.Values.ToList().ForEach(x => x.Parse());
  112.        
  113.         //save
  114.         allGuards = guards.Values.ToArray();
  115.        
  116.         //D();
  117.     }
  118.  
  119.     public static int AnswerA()
  120.     {
  121.         //find guard that is the most minutes asleep
  122.         var sleepyGuard = allGuards.OrderByDescending(x => x.countMinutesAsleep).First();
  123.  
  124.         return sleepyGuard.minuteSleepingTheMost * int.Parse(sleepyGuard.id);
  125.     }
  126.  
  127.     public static int AnswerB()
  128.     {      
  129.         //ding the guard that sleeps the most at a specific minute
  130.         var sleepyGuard = allGuards.OrderByDescending(x => x.maxSleepingCount).First();
  131.  
  132.         return sleepyGuard.minuteSleepingTheMost * int.Parse(sleepyGuard.id);
  133.     }  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment