Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main()
- {
- //test
- var input = new string[]{
- #region
- "[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 ",
- #endregion
- };
- Guard.CreateGuards(input);
- Debug.Assert(Guard.AnswerA() == 240);
- Debug.Assert(Guard.AnswerB() == 4455);
- //create aoc
- var aoc = new AdventOfCode(2018, 4, Login.Google);
- //sort lines
- input = aoc.InputLines;
- //create guards
- Guard.CreateGuards(aoc.InputLines);
- //get answers
- aoc.SubmitAnswer(Guard.AnswerA(), Part.A);
- aoc.SubmitAnswer(Guard.AnswerB(), Part.B);
- }
- class Guard
- {
- private static Guard[] allGuards;
- //id of the guards
- public string id;
- //track all sleeping minutes
- public int[] sleep = new int[60];
- public int countMinutesAsleep;
- public int minuteSleepingTheMost;
- public int maxSleepingCount => sleep[minuteSleepingTheMost];
- public int[] Parse()
- {
- countMinutesAsleep = sleep.Sum(); //count all the minutes asleep
- minuteSleepingTheMost = sleep
- .Select((x, index) => (x, index)) //create index/minute
- .OrderByDescending(y=> y.x).First().index; //sort the minute that is most often asleep
- return sleep;
- }
- public static void CreateGuards(string[] lines)
- {
- var guards = new Dictionary<string, Guard>();
- Guard activeGuard = null; //current guard
- lines = lines.OrderBy(x => x).ToArray();
- for(int i = 0; i < lines.Length; i++)
- {
- var match = Regex.Match(lines[i], @"Guard #(\d+)"); //get id
- if (match.Success)
- {
- //create new guard?
- if (guards.ContainsKey(match.Groups[1].Value) == false)
- {
- activeGuard = new Guard() { id = match.Groups[1].Value };
- //add to dic
- guards.Add(activeGuard.id, activeGuard);
- }
- //get guard
- activeGuard = guards[match.Groups[1].Value];
- }
- else if (lines[i].Contains("asleep") && lines[i + 1].Contains("wakes up"))
- {
- var start = int.Parse(lines[i].Substring(15, 2)); //get start minute
- var end = int.Parse(lines[i + 1].Substring(15, 2)); //endMinute
- while (start != end)
- {
- //increment minute and increment minute counter in sleep
- activeGuard.sleep[start++]++;
- }
- }
- }
- //parse the content for the guards
- guards.Values.ToList().ForEach(x => x.Parse());
- //save
- allGuards = guards.Values.ToArray();
- //D();
- }
- public static int AnswerA()
- {
- //find guard that is the most minutes asleep
- var sleepyGuard = allGuards.OrderByDescending(x => x.countMinutesAsleep).First();
- return sleepyGuard.minuteSleepingTheMost * int.Parse(sleepyGuard.id);
- }
- public static int AnswerB()
- {
- //ding the guard that sleeps the most at a specific minute
- var sleepyGuard = allGuards.OrderByDescending(x => x.maxSleepingCount).First();
- return sleepyGuard.minuteSleepingTheMost * int.Parse(sleepyGuard.id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment