public int CompareTo(object obj) { ShiftEvent e = (ShiftEvent)obj; int year = Year.CompareTo(e.Year); if (year != 0) { return(year); } int month = Month.CompareTo(e.Month); if (month != 0) { return(month); } int day = Day.CompareTo(e.Day); if (day != 0) { return(day); } return(Minute.CompareTo(e.Minute)); }
//"[1518-11-01 00:00] Guard #10 begins shift" // 0 1 2 3 4 5 // //"[1518-11-01 00:05] falls asleep" // 0 1 2 3 // //"[1518-11-02 00:50] wakes up" // 0 1 2 3 public static ShiftEvent ParseEvent(string input) { var parts = input.Split(new[] { '[', ']', ' ' }, StringSplitOptions.RemoveEmptyEntries); var eventTime = DateTime.ParseExact(parts[0] + " " + parts[1], "yyyy-MM-dd HH:mm", CultureInfo.CurrentCulture); switch (parts[2]) { case "Guard": { var guardId = int.Parse(parts[3].Replace("#", "")); var evt = new ShiftEvent(ShiftEventTypes.ShiftStart, eventTime, guardId); return(evt); } case "falls": { var evt = new ShiftEvent(ShiftEventTypes.FallAsleep, eventTime, -1); return(evt); } case "wakes": { var evt = new ShiftEvent(ShiftEventTypes.WakeUp, eventTime, -1); return(evt); } } throw new Exception("Unable to parse: " + input); }
public void Init() { // Parse the event _input = Aoc.Framework.Input.GetStringVector(this); _events = new ShiftEvent[_input.Length]; for (int i = 0; i < _input.Length; ++i) { string[] items = _input[i].Split("]"); string[] timestamp = items[0].Substring(1).Split('-', ' ', ':'); int? guard = null; bool awake = false; int day = int.Parse(timestamp[2]) + (timestamp[3] == "23" ? 1 : 0); int minute = timestamp[3] == "23" ? 0 : int.Parse(timestamp[4]); string[] content = items[1].Trim().Split(" "); if (content[0] == "Guard") { awake = true; guard = int.Parse(content[1].Substring(1)); minute = 0; } if (content[0] == "wakes") { awake = true; } _events[i] = new ShiftEvent { Year = int.Parse(timestamp[0]), Month = int.Parse(timestamp[1]), Day = day, Minute = minute, Guard = guard, Awake = awake }; } // Sort them Array.Sort(_events); // Resolve the guards id int?currentGuard = null; for (int i = 0; i < _events.Length; ++i) { if (_events[i].Guard.HasValue) { currentGuard = _events[i].Guard.Value; } else { _events[i].Guard = currentGuard; } } }
public static Shift StartShift(this ShiftEvent shiftEvent, Tuple <DateTime, DateTime> interval) { return(new Shift { EmployeeId = shiftEvent.EmpId, StartOfShiftDate = interval.Item1, EndOfShiftDate = interval.Item2, ShiftEvents = new List <ShiftEvent> { shiftEvent } }); }
protected bool Equals(ShiftEvent other) { return(EventType == other.EventType && EventTime.Equals(other.EventTime) && GuardId == other.GuardId); }
public static Tuple <DateTime, DateTime> GetShiftInterval(this ShiftEvent shiftEvent) { return(new Tuple <DateTime, DateTime>(ParseDateTime(shiftEvent.StartDate, shiftEvent.StartTime), ParseDateTime(shiftEvent.EndDate, shiftEvent.EndTime))); }
public ShiftEntry(int id, ShiftEvent shiftEvent, DateTime date) { Id = id; Event = shiftEvent; Date = date; }