-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-parse.c
181 lines (151 loc) · 3.59 KB
/
cloud-parse.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif
/* Window duration in minutes */
#define WIN_TIME 60
/* Number of slots in window array (worst case 2 slots per minute) */
#define WIN_SLOTS (WIN_TIME * 2)
/* Maximum height (meters) to assume when height value is not available */
#define MAX_HEIGHT 10000
struct day {
char date[12];
uint32_t min;
} days[365];
/* Meters to feet conversion */
static uint32_t mtof(uint32_t m)
{
return (uint32_t)((double)m * 3.2808399);
}
static int entry_cmp(const void *a, const void *b)
{
const uint32_t *entry1 = a;
const uint32_t *entry2 = b;
return (int)*entry1 - (int)*entry2;
}
static uint32_t new_min(uint32_t win[WIN_SLOTS], uint32_t slot_count, uint32_t old_min)
{
if (slot_count == 0) {
return old_min;
}
qsort(win, slot_count, sizeof(win[0]), entry_cmp);
uint32_t new_min = win[slot_count / 2];
if (new_min < old_min) {
return new_min;
}
return old_min;
}
static bool win_complete(uint32_t win_time, uint32_t slot_count)
{
return win_time > WIN_TIME || slot_count >= WIN_SLOTS;
}
int main(int argc, char *argv[])
{
char date[16], time[16], type[16], height_str[16];
char cur_date[16] = "";
uint32_t day_count = 0;
uint32_t below_2000 = 0, below_1000 = 0, below_500 = 0,
below_400 = 0, below_300 = 0, below_200 = 0;
uint32_t slot = 0;
uint32_t slot_start = 0;
uint32_t day_min = MAX_HEIGHT;
struct day *day = NULL;
uint32_t unix;
while (true) {
uint32_t win[WIN_SLOTS];
uint32_t win_time;
uint32_t height;
if (fscanf(stdin, "%s %s %s %u.%*u %s",
date, time, type, &unix, height_str) != 5) {
if (day && !win_complete(win_time, slot)) {
strcpy(day->date, cur_date);
day->min = day_min;
}
break;
}
if (slot_start != 0) {
win_time = unix - slot_start;
} else {
win_time = 0;
}
if (strcmp(height_str, "None") == 0) {
height = MAX_HEIGHT;
} else {
height = strtol(height_str, NULL, 0);
}
/* New day starting */
if (strcmp(cur_date, date) != 0) {
if (day) {
/* Update statistics for previous day */
if (!win_complete(win_time, slot)) {
day_min = new_min(win, slot, day_min);
}
strcpy(day->date, cur_date);
day->min = day_min;
}
strcpy(cur_date, date);
slot = 0;
day_min = MAX_HEIGHT;
slot_start = unix;
if (day_count < ARRAY_SIZE(days)) {
day = &days[day_count++];
} else {
day = NULL;
printf("Too small days array!\n");
}
}
if (slot == WIN_SLOTS) {
if (win_time > WIN_TIME) {
slot_start = unix;
slot = 0;
win_time = 0;
} else {
printf("Too few window slots!\n");
continue;
}
} else if (win_time > WIN_TIME) {
day_min = new_min(win, slot, day_min);
slot_start = unix;
slot = 0;
win_time = 0;
}
if (win_time <= WIN_TIME && slot < WIN_SLOTS) {
win[slot++] = height;
if (slot == WIN_SLOTS) {
day_min = new_min(win, slot, day_min);
continue;
}
}
}
for (int i = 0; i < day_count; i++) {
day = &days[i];
uint32_t f = mtof(day->min);
if (f < 2000) {
below_2000++;
if (f < 1000) {
below_1000++;
if (f < 500) {
below_500++;
if (f < 400) {
below_400++;
if (f < 300) {
below_300++;
if (f < 200) {
below_200++;
}
}
}
}
}
}
printf("%s %u\n", day->date, f);
}
printf("\n%u days total\n", day_count);
printf("< 2000: %u, < 1000: %u, < 500: %u, < 400: %u, < 300: %u, < 200: %u\n",
below_2000, below_1000, below_500, below_400, below_300, below_200);
return 0;
}