static GuardLog ParseLogEntry(string input) { var splitResult = input.Split(']'); var time = DateTime.Parse(splitResult[0].Trim().Trim('[')); GuardEventType eventType = GuardEventType.ShiftStart; int id = 0; if (splitResult[1].Trim() == "falls asleep") { eventType = GuardEventType.FallAsleep; } else if (splitResult[1].Trim() == "wakes up") { eventType = GuardEventType.WakeUp; } else { var indexOfHash = splitResult[1].IndexOf('#'); var endOfId = splitResult[1].IndexOf(' ', indexOfHash); id = int.Parse(splitResult[1].Substring(indexOfHash + 1, endOfId - indexOfHash)); } return(new GuardLog { Time = time, EventType = eventType, GuardId = id }); }
public GuardEvent(string details) { var matches = details.Match(@"\[(.+)\] (.+)"); if (matches.Groups.Count != 3) { Oh.Bugger(); } Time = DateTime.ParseExact(matches.Groups[1].Value, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture); var desription = matches.Groups[2].Value; if (desription == "falls asleep") { Type = GuardEventType.Asleep; Id = -1; } else if (desription == "wakes up") { Type = GuardEventType.Awake; Id = -1; } else { Type = GuardEventType.BeginShift; Id = int.Parse(desription.Match(@"#(\d+)").Groups[1].Value); } }
public void Test_EventParsing(string details, GuardEventType expectedType, string expectedTime, int expectedId) { var time = DateTime.Parse(expectedTime); var ev = new GuardEvent(details); ev.Type.Should().Be(expectedType); ev.Time.Should().Be(time); ev.Id.Should().Be(expectedId); }