-
Notifications
You must be signed in to change notification settings - Fork 2
/
04.c
188 lines (158 loc) · 4.18 KB
/
04.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "adventofcode.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define PUZZLE_NAME "Day 4: Repose Record"
struct log_entry {
int year;
int month;
int day;
int hour;
int minute;
int guard_id;
int awake;
};
struct guard {
int id;
int minutes[60];
int total_sleep_time;
};
static int log_entry_compare(const void *p1, const void *p2) {
const struct log_entry *a = (struct log_entry *)p1;
const struct log_entry *b = (struct log_entry *)p2;
if (a->year < b->year) {
return -1;
}
if (a->year == b->year) {
if (a->month < b->month) {
return -1;
}
if (a->month == b->month) {
if (a->day < b->day) {
return -1;
}
if (a->day == b->day) {
if (a->hour < b->hour) {
return -1;
}
if (a->hour == b->hour) {
return a->minute - b->minute;
}
}
}
}
return 1;
}
static int parse(struct log_entry *log_entries, const char *s) {
int nentries = 0;
while (*s != 0x0) {
struct log_entry *e = &log_entries[nentries++];
e->guard_id = 0;
e->awake = 0;
sscanf(s, "[%d-%02d-%02d %02d:%02d]", &e->year, &e->month, &e->day,
&e->hour, &e->minute);
s += 19; // skip timestamp
switch (*s) {
case 'G':
sscanf(s, "Guard #%d", &e->guard_id);
break;
case 'f':
e->awake = 0;
break;
case 'w':
e->awake = 1;
break;
}
while (*s != '\n' && *s != 0x0)
s++;
if (*s == '\n')
s++;
}
qsort(log_entries, (size_t)nentries, sizeof(struct log_entry),
log_entry_compare);
return nentries;
}
static struct guard *guard_by_id(struct guard *guards, const int nguards, const int id) {
for (int i = 0; i < nguards; i++) {
if (guards[i].id == id) {
return &guards[i];
}
}
return NULL;
}
static int log_entries_into_guards(struct guard *guards, struct log_entry *entries,
const int nentries) {
int nguards = 0;
struct guard *guard = NULL;
for (int i = 0; i < nentries; i++) {
struct log_entry e = entries[i];
if (e.guard_id > 0) {
guard = guard_by_id(guards, nguards, e.guard_id);
if (guard == NULL) {
guard = &guards[nguards++];
guard->id = e.guard_id;
guard->total_sleep_time = 0;
memset(guard->minutes, 0, 60 * sizeof(*guard->minutes));
}
} else if (e.awake > 0) {
if (i == 0 || guard == NULL) {
fprintf(stderr, "logic error");
exit(EXIT_FAILURE);
}
struct log_entry prev = entries[i - 1];
guard->total_sleep_time += (e.minute - prev.minute);
for (int m = prev.minute; m < e.minute; m++) {
guard->minutes[m]++;
}
}
}
return nguards;
}
static int solve_pt1(struct guard *guards, const int nguards) {
struct guard *guard = &guards[0];
for (int i = 0; i < nguards; i++) {
if (guards[i].total_sleep_time > guard->total_sleep_time) {
guard = &guards[i];
}
}
int sleepy_minute = 0;
for (int m = 0; m < 60; m++) {
if (guard->minutes[m] > guard->minutes[sleepy_minute]) {
sleepy_minute = m;
}
}
int answer_pt1 = guard->id * sleepy_minute;
return answer_pt1;
}
static int solve_pt2(struct guard *guards, const int nguards) {
struct guard *guard = &guards[0];
int max = 0;
for (int i = 0; i < nguards; i++) {
for (int m = 0; m < 60; m++) {
if (guards[i].minutes[m] > guard->minutes[max]) {
guard = &guards[i];
max = m;
}
}
}
return guard->id * max;
}
int main(void) {
clock_t t = clock_time();
char input[1024 * 64] = "";
read_input_file(input, 1024 * 64, "04.txt");
struct log_entry *log_entries = malloc_or_die(2048 * sizeof(struct log_entry));
int nentries = parse(log_entries, input);
struct guard *guards = malloc_or_die(512 * sizeof(struct guard));
int nguards = log_entries_into_guards(guards, log_entries, nentries);
int pt1 = solve_pt1(guards, nguards);
int pt2 = solve_pt2(guards, nguards);
printf("--- %s ---\n", PUZZLE_NAME);
printf("Part 1: %d\n", pt1);
printf("Part 2: %d\n", pt2);
printf("Time: %.2fms\n", clock_time_since(t));
free(guards);
free(log_entries);
return EXIT_SUCCESS;
}