-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (118 loc) Β· 4.23 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
package main
import (
"io/ioutil"
"log"
"regexp"
"sort"
"strconv"
"strings"
)
func main() {
input, err := ioutil.ReadFile("input.txt")
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
lines := strings.Split(string(input), "\n")
id, minute := FindGuardMostAsleep(lines)
log.Printf("Guard %d sleeps the most. The minute he is most asleep is: %d. %d * %d = %d\n", id, minute, id, minute, id*minute)
id, minute = FindGuardMostFrequentlyAsleep(lines)
log.Printf("Guard %d is asleep most frequently on the same minute (minute %d). %d * %d = %d\n", id, minute, id, minute, id*minute)
}
// Schedule contains a count of times a guard is asleep for each minute (0-59) for each guard.
//
// In case of the example it will look like this:
//
// |00 01 02 03 04 05 06 07 08 09 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
//
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// 10 | 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
// 99 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 2 2 2 2 2 3 2 2 2 2 1 1 1 1 1 0 0 0 0 0
type Schedule map[int][]int
// Statistics describe the sleep patterns of a single guard.
type Statistics struct {
TotalMinutes int
MaxFrequency int
MinuteMostFrequent int
}
// FindGuardMostAsleep looks for the guard that is asleep the most.
// It returns the ID of the found guard, together with the minute that he is asleep most frequently on.
func FindGuardMostAsleep(input []string) (guard int, minute int) {
schedule := createSchedule(input)
stats := createStatistics(schedule)
maxTotal := 0
for id, s := range stats {
if s.TotalMinutes > maxTotal {
guard = id
maxTotal = s.TotalMinutes
}
}
return guard, stats[guard].MinuteMostFrequent
}
// FindGuardMostFrequentlyAsleep looks for the guard that is most frequently asleep on the same minute.
// It returns the ID of the found guard, together with the minute that he is asleep most frequently on.
func FindGuardMostFrequentlyAsleep(input []string) (guard int, minute int) {
schedule := createSchedule(input)
stats := createStatistics(schedule)
maxFrequency := 0
for id, s := range stats {
if s.MaxFrequency > maxFrequency {
guard = id
maxFrequency = s.MaxFrequency
}
}
return guard, stats[guard].MinuteMostFrequent
}
// createStatistics converts the given schedule to a map of statistics per guard.
func createStatistics(schedule Schedule) map[int]Statistics {
result := make(map[int]Statistics)
for guardId, minutes := range schedule {
stats := Statistics{}
for minute, frequency := range minutes {
if frequency > stats.MaxFrequency {
stats.MaxFrequency = frequency
stats.MinuteMostFrequent = minute
}
stats.TotalMinutes += frequency
}
result[guardId] = stats
}
return result
}
// createSchedule takes the given input and creates a schedule from it.
func createSchedule(input []string) Schedule {
records := make([]string, len(input))
for i := range input {
records[i] = input[i]
}
sort.Strings(records)
minute := 0
guard := -1
asleep := false
schedule := make(Schedule)
newShiftPattern := regexp.MustCompile(".*Guard #(\\d+) begins shift")
for len(records) > 0 {
record := records[0]
currentMinute, _ := strconv.Atoi(record[15:17])
if minute == 0 && newShiftPattern.MatchString(record) {
match := newShiftPattern.FindStringSubmatch(record)
guard, _ = strconv.Atoi(match[1])
records = records[1:]
if schedule[guard] == nil {
schedule[guard] = make([]int, 60)
}
}
if strings.Contains(record, "falls asleep") && currentMinute == minute {
asleep = true
records = records[1:]
}
if strings.Contains(record, "wakes up") && currentMinute == minute {
asleep = false
records = records[1:]
}
if asleep {
schedule[guard][minute] = schedule[guard][minute] + 1
}
minute = (minute + 1) % 60
}
return schedule
}