This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprisoners-solution-2.c
140 lines (116 loc) · 2.88 KB
/
prisoners-solution-2.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PRISONER_NUM 100
// https://www.tiktok.com/@evan_db_dub/video/7186126660859645226
typedef enum {
BOOL_TRUE = 1,
BOOL_FALSE = 0
} boolean;
struct prisoner {
int count;
int found_on;
int found_off;
int times_flipped;
int visits;
};
struct prisoner prison[PRISONER_NUM];
boolean lightswitch, start;
int count, day, finish;
void flip() {
if (lightswitch == BOOL_TRUE) {
lightswitch = BOOL_FALSE;
} else {
lightswitch = BOOL_TRUE;
}
//fprintf(stdout,"SWITCH FLIPPED\n");
return;
}
void assert() {
if (count == PRISONER_NUM) {
fprintf(stdout,"WIN after %d days\n",day);
} else {
fprintf(stdout,"LOSE after %d days -- count was %d\n",day,count);
}
finish = BOOL_TRUE;
}
void interrogate(int cell) {
boolean found = lightswitch;
//fprintf(stdout,"day %d: called prisoner %d; light was %s for the %dth time (real count %d)\n",day,cell,(lightswitch == BOOL_TRUE ? "ON" : "OFF"),(lightswitch == BOOL_TRUE ? prison[cell].found_on+1 : prison[cell].found_off+1),count);
/*
===============================
PRISONER STRATEGY
===============================
https://www.tiktok.com/@evan_db_dub/video/7187605933026086190
https://www.tiktok.com/@evan_db_dub/video/7187627231923948842
*/
if (cell == 0) {
//Designated Counter
//==================
if (lightswitch == BOOL_TRUE) {
prison[cell].count++;
flip();
prison[cell].times_flipped++;
//fprintf(stdout,"flipped: %d; count: %d\n",prison[cell].times_flipped,prison[cell].count);
if (prison[cell].count == 2*(PRISONER_NUM - 1)) {
assert();
}
}
//=================
} else {
//General Prisoners
//=================
if (lightswitch == BOOL_FALSE // CONDITION ONE
&& // AND
prison[cell].times_flipped < 2) // CONDITION TWO
{
flip();
prison[cell].times_flipped++;
}
//=================
}
/*
===============================
===============================
*/
if (prison[cell].visits == 0) {
count++;
}
prison[cell].visits++;
if (found == BOOL_TRUE) {
prison[cell].found_on++;
} else {
prison[cell].found_off++;
}
return;
}
void init() {
int i = 0;
srandom(time(0));
if (random()%2) {
lightswitch = BOOL_TRUE;
} else {
lightswitch = BOOL_FALSE;
}
start = lightswitch;
//fprintf(stdout,"light starting %s\n",(lightswitch == BOOL_TRUE ? "ON" : "OFF"));
count = 0;
day = 0;
for (i=0; i < PRISONER_NUM; i++) {
prison[i].count = 0;
prison[i].found_on = 0;
prison[i].found_off = 0;
prison[i].times_flipped = 0;
prison[i].visits = 0;
}
finish = BOOL_FALSE;
}
int main (int argc, char **argv) {
init();
while (finish == BOOL_FALSE) {
interrogate(random() % PRISONER_NUM);
day++;
}
fprintf(stdout,"light started %s; prisoner 0 has been called %d times\n",(start == BOOL_TRUE ? "ON" : "OFF"),prison[0].visits);
return 0;
}