adventofcode/2018/day04-1.go

46 lines
914 B
Go
Raw Normal View History

2018-12-04 08:23:19 +00:00
package main
import (
"fmt"
"internal/guards"
2018-12-04 08:23:19 +00:00
"internal/util"
)
func main() {
data := util.ReadInput()
shifts := guards.ParseInput(data)
guards := guards.BuildGuards(shifts)
2018-12-04 08:23:19 +00:00
sleepiest, sleepiestMinute := FindSleepiestData(guards)
fmt.Printf("Sleepiest Guard: %d\n", sleepiest.ID)
2018-12-04 08:23:19 +00:00
fmt.Printf("Sleepiest Minute: %d\n", sleepiestMinute)
fmt.Printf("Result: %d\n", sleepiest.ID*sleepiestMinute)
2018-12-04 08:23:19 +00:00
}
func FindSleepiestData(data map[int]*guards.Guard) (*guards.Guard, int) {
var sleepiest *guards.Guard
for _, guard := range data {
2018-12-04 08:23:19 +00:00
if sleepiest == nil {
sleepiest = guard
continue
}
if guard.SleepTotal > sleepiest.SleepTotal {
2018-12-04 08:23:19 +00:00
sleepiest = guard
}
}
sleepiestMinute := -1
sleepiestCount := 0
for minute, sleepiness := range sleepiest.Sleep {
2018-12-04 08:23:19 +00:00
if sleepiness > sleepiestCount {
sleepiestCount = sleepiness
sleepiestMinute = minute
}
}
return sleepiest, sleepiestMinute
}