Another fun problem today.

I started the day with some refactoring of my code from the last three days ensuring everything still ran correctly with my unit tests, moving the creation of elements to a DataProvider interface which is implemented by different classes depending on the type wanted (int, string, claim, and now guard). This also moved the regular expression part of the input handling to the data providers instead of the problem solvers, making the unit tests for the solvers easier and the unit tests for the data providers more complete.

My guard class contains the id of the guard and a list of all the intervals where the guard slept. It was pretty easy to build the data by first sorting the lines in the file, as the timestamp was in a easily sortable format.

The solution to the first part of the problem is to find the guard with the most minutes asleep (by summing all the intervals for each guard), and then find the minute where he was most asleep.

The solution to the second part of the problem is just a invert in the steps for the solution to the first part. Find the minute at which each guard slept the most and then find the guard that slept the most out of those minutes.

The code is available in my github repository: advent solutions code and to those interested, check the commit for today as it is a nice exercise in refactoring.