-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_crystal_balls.c
68 lines (57 loc) · 1.77 KB
/
two_crystal_balls.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
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef enum TwoCrystalBallsError { OK, DID_NOT_BREAK } TwoCrystalBallsError;
TwoCrystalBallsError two_crystal_balls(bool *break_points,
size_t break_points_size, size_t *out);
char *error_message_two_crystal_balls(TwoCrystalBallsError error);
int main(void) {
bool break_points[] = {
false, false, false, false, false, false, false, false,
false, true, true, true, true, true, true, true,
};
size_t break_points_size = sizeof(break_points) / sizeof(*break_points);
size_t floor;
TwoCrystalBallsError two_crystal_balls_error;
if ((two_crystal_balls_error =
two_crystal_balls(break_points, break_points_size, &floor)) != OK) {
fprintf(stderr, "invalid break points, error: %s\n",
error_message_two_crystal_balls(two_crystal_balls_error));
exit(EXIT_FAILURE);
}
printf("the crystal balls break on floor: %lu\n", floor);
return 0;
}
TwoCrystalBallsError two_crystal_balls(bool *break_points,
size_t break_points_size, size_t *out) {
size_t jump_size = (size_t)floor(sqrt(break_points_size));
size_t i = jump_size;
while (i < break_points_size) {
if (break_points[i]) {
break;
}
i += jump_size;
}
size_t j = i - jump_size;
while (j <= i && i < break_points_size) {
if (break_points[j]) {
*out = j;
return OK;
}
++j;
++i;
}
return DID_NOT_BREAK;
}
char *error_message_two_crystal_balls(TwoCrystalBallsError error) {
switch (error) {
case OK:
return "no error";
case DID_NOT_BREAK:
return "crystal balls do not break with these break points";
default:
return "unknown error";
}
}