-
Notifications
You must be signed in to change notification settings - Fork 0
/
guardschedules.go
90 lines (69 loc) · 1.67 KB
/
guardschedules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package day04_2018
import (
"log"
"time"
)
const (
START_SHIFT = iota
FALL_ASLEEP
WAKE_UP
)
type GuardEvent struct {
GuardID int
EventType int
DateTime time.Time
}
type GuardSchedule struct {
ID int
TotalTimeAsleep int
SleepFrequency [60]int
}
type GuardSchedules struct {
Schedules map[int]*GuardSchedule
}
func (gs GuardSchedules) RegisterSleepTime(guardID int, sleepStart time.Time, sleepEnd time.Time) {
minuteStart, minuteEnd := sleepStart.Minute(), sleepEnd.Minute()
for i := minuteStart; i < minuteEnd; i++ {
if gs.Schedules[guardID] == nil {
gs.Schedules[guardID] = &GuardSchedule{}
}
gs.Schedules[guardID].SleepFrequency[i]++
gs.Schedules[guardID].TotalTimeAsleep++
}
}
func (gs GuardSchedules) RegisterSleepTimeFromEvents(events []GuardEvent) {
currentGID := 0
isAsleep := false
var sleepStart time.Time
for _, guardEvent := range events {
switch guardEvent.EventType {
case FALL_ASLEEP:
isAsleep = true
sleepStart = guardEvent.DateTime
case WAKE_UP:
if isAsleep {
isAsleep = false
sleepEnd := guardEvent.DateTime
guardEvent.GuardID = currentGID
gs.RegisterSleepTime(currentGID, sleepStart, sleepEnd)
} else {
log.Printf("Waking up, but it was not asleep!")
}
case START_SHIFT:
isAsleep = false
currentGID = guardEvent.GuardID
default:
log.Printf("Event not recognized!")
}
}
}
func (gs GuardSchedules) GetMostSleepyGuard() int {
currentMax, currentGuardID := -1, -1
for guardID, guardSchedule := range gs.Schedules {
if guardSchedule.TotalTimeAsleep > currentMax {
currentMax = guardSchedule.TotalTimeAsleep
currentGuardID = guardID
}
}
return currentGuardID
}