AOC18: Repose Record
Advent of Code 2018 Day 4
It’s that time of year again, Christmas is around the corner and Eric Wastl has gifted us with another set of Advent of Code puzzles.
Welcome to Advent Of Code 2018. You can read Part 1 of this series Here or see all the parts of this series Here.
Part 1
Day 4 starts again with reading some data from a text file. Today we have a list of log entries for a Guard post. We have entries for when a guard comes on duty, falls asleep and wakes up again. Our task is to sort the list of entries by date and time, and find what guard sleeps the most.
Our first steps as always is read the file into a slice of lines.
|
|
Next we need to have a way store and sort our entries, since the elves were so helpful to not store them in chronological (or any) order. We’ll create a new data struct (logEntry), which will contain the parsed time of the entry, the raw entry text, the type of entry, the ID of the guard and the sleeping state of the entry. Just for fun, we’ll also create a logType datatype and use some named consts using the iota function.
|
|
Now, we’ll create a slice of LogEntries, and iterate over the list from the file. For each line, we’ll parse the time using time.Parse, and create our logEntry, filling just the Time and Entry. After sorting we’ll be able to fill in the other information.
|
|
To sort our slice we can utilize the sort package’s sort.Slice method, all we need to do is provide a way for sort.Slice to determine if one item is less than the next. We can use the time.Time.Before to accomplish this.
|
|
Next we’ll re-read our log entries in order, since the sleep and wake entries don’t tell us the current guard ID, we’ll have to track the last seen guard ID. We’ll take a note from yesterday’s puzzle and use a regular expression to find the guard ID from the begin shift entries. Once we have our information, we’ll update the Log slice.
|
|
Next we need a way to track the number of minutes each guard slept, as well as tallying the sleeps by minute. We’ll define a new guardLog struct with a SleepTally integer, and a map of ints for tracking what minutes the guard slept.
|
|
We’ll use this new struct in a map using the guard’s ID as the key. Next we’ll iterate over our log once again, tracking when the last fall asleep entry was, and when we get a wake up entry we’ll either create a new guardLog, or use an existing guardLog if we’ve seen this guard already. We’ll use a for loop to increment the by minute tally, then calculate the total amount of time slept. Then we’ll store the new (or updated) guardLog back to our Guards map.
|
|
Now we can finally figure out which of the guards slept the most, and in which minute he slept the most. Multiplying those two values together gives us our answer for part 1.
|
|
Part 2
The next part what guard slept the most frequently on the same minute. We can simply add a couple blocks of code to our last section to find this information, getting the answer for part 2.
|
|
The complete code for this puzzle is available, along with the other days, on my Gitlab repository https://gitlab.com/aarynsmith/adventofcode/blob/master/2018/Day_4/Day4.go
If you have any questions or comments, please feel free comment below.