Advertisement
Blacksilver_

100 prisoners & a lightbulb (ungolfed)

Oct 24th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <time.h>
  5. #define LEADER 0
  6.  
  7.  
  8. bool been_in_room[100];
  9. bool light = false;
  10.  
  11. inline int pick(){
  12. int n = (int)random() % 100;
  13. printf("Prisoner %d is picked.\n",n);
  14. return n;
  15. }
  16.  
  17. int main(){
  18. int i,p;
  19. srandom(time(NULL)); //set seed to current unixtime
  20.  
  21. //main program loop
  22. for(i=0;i<100;i++){
  23. start:
  24. p = (int)random() % 100;
  25. if(p == LEADER){
  26. printf("Prisoner %02d (LEADER) is picked.\n",p);
  27. if(light == true){
  28. printf("The leader turns the light off.\n");
  29. light = false;
  30. continue;
  31. } else {
  32. goto start;
  33. }
  34. } else {
  35. printf("Prisoner %02d is picked.\n",p);
  36. }
  37.  
  38. if(been_in_room[p] == true){
  39. //he's been here before, walk out.
  40. printf(" Prisoner %d has been in the room.\n",p);
  41. goto start;
  42. } else {
  43. //he hasn't been here before, light on?
  44. if(light == true){
  45. printf(" Prisoner %02d has not been in the room, but the light is on.\n",p);
  46. goto start;
  47. } else {
  48. light = true;
  49. printf(" Prisoner %02d turns the light on.\n",p);
  50. goto start;
  51. }
  52. }
  53. }
  54. printf("Prisoner %02d declares that all prisoners have been in the room.\n",LEADER);
  55. for(i=0;i<100;i++){
  56. if(been_in_room == false){
  57. printf("they are all killed. Prisoner %d has not been in the room.\n",i);
  58. exit(1);
  59. }
  60. }
  61. puts("Good job!");
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement