From 1615406a28d607f0340df55f513338a326e0998f Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Tue, 4 Dec 2018 03:34:23 -0500 Subject: [PATCH] Day 4 solution, part 2. --- 2018/day04-2.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2018/day04-2.go diff --git a/2018/day04-2.go b/2018/day04-2.go new file mode 100644 index 0000000..6856867 --- /dev/null +++ b/2018/day04-2.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + + "internal/guards" + "internal/util" +) + +func main() { + data := util.ReadInput() + shifts := guards.ParseInput(data) + guards := guards.BuildGuards(shifts) + + sleepiest, sleepiestMinute := FindSleepiestData(guards) + + fmt.Printf("Sleepiest Guard: %d\n", sleepiest.ID) + fmt.Printf("Sleepiest Minute: %d\n", sleepiestMinute) + fmt.Printf("Result: %d\n", sleepiest.ID*sleepiestMinute) +} + +func FindSleepiestData(data map[int]*guards.Guard) (*guards.Guard, int) { + var sleepiest *guards.Guard + var minute, value int + + for _, guard := range data { + for i := 0; i < 60; i++ { + if guard.Sleep[i] > value { + sleepiest = guard + minute = i + value = guard.Sleep[i] + } + } + } + + return sleepiest, minute +}