-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
156 lines (133 loc) · 2.59 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bufio"
"io"
"sort"
"time"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
var actions []Action
sleeps := make(map[int][]int, 0)
for scanner.Scan() {
line := scanner.Text()
action := toAction(line)
actions = append(actions, action)
sleeps[action.id] = make([]int, 60)
}
sort.Slice(actions, func(i, j int) bool {
return actions[i].t.Before(actions[j].t)
})
id := 0
for i := 0; i < len(actions); i++ {
action := actions[i]
if action.actionType == BeginShift {
id = action.id
continue
}
start := action.t.Minute()
end := actions[i+1].t.Minute()
i++
for t := start; t < end; t++ {
sleeps[id][t]++
}
}
id = -1
mostMinutesAsleep := 0
for k, minutes := range sleeps {
total := 0
for minute := 0; minute < len(minutes); minute++ {
total += minutes[minute]
}
if total > mostMinutesAsleep {
mostMinutesAsleep = total
id = k
}
}
max := 0
minute := -1
for i, m := range sleeps[id] {
if m > max {
max = m
minute = i
}
}
return id * minute
}
type Action struct {
t time.Time
actionType ActionType
id int
}
func toAction(s string) Action {
t, err := time.Parse("2006-01-02 15:04", s[1:17])
if err != nil {
panic(err)
}
var actionType ActionType
id := 0
switch s[19] {
case 'w':
actionType = WakesUp
case 'f':
actionType = FallsAsleep
case 'G':
actionType = BeginShift
del := lib.NewDelimiter(s, " ")
s := del.GetString(3)
id = lib.StringToInt(s[1:])
}
return Action{
t: t,
actionType: actionType,
id: id,
}
}
type ActionType int
const (
BeginShift ActionType = iota
FallsAsleep
WakesUp
)
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
var actions []Action
sleeps := make(map[int][]int, 0)
for scanner.Scan() {
line := scanner.Text()
action := toAction(line)
actions = append(actions, action)
sleeps[action.id] = make([]int, 60)
}
sort.Slice(actions, func(i, j int) bool {
return actions[i].t.Before(actions[j].t)
})
id := 0
for i := 0; i < len(actions); i++ {
action := actions[i]
if action.actionType == BeginShift {
id = action.id
continue
}
start := action.t.Minute()
end := actions[i+1].t.Minute()
i++
for t := start; t < end; t++ {
sleeps[id][t]++
}
}
maxMinuteAsleep := -1
chosenId := 0
chosenMinute := 0
for id, minutes := range sleeps {
for minute, total := range minutes {
if total > maxMinuteAsleep {
maxMinuteAsleep = total
chosenId = id
chosenMinute = minute
}
}
}
return chosenMinute * chosenId
}