@LevMckinney/

Pigs and buckets

C++11

No description

fork
loading
Files
  • main.cpp
main.cpp
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
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>

using namespace std;

class Pig
{
  private:
    bool pPoisoned;
    bool pDead;
    int pPoisonBucket;
  
  public:
    Pig(int poisonBucket)
    {
      pDead = false;
      pPoisoned = false;
      pPoisonBucket = poisonBucket;
    }
    
    void Drink(int bucket)
    {
      if(pPoisonBucket == bucket)
        pPoisoned = true;
    }
    
    bool Dead()
    {
      return pDead;
    }
    
    //this is the only one you cant touch. No Cheating!!!!!!!!
    void Step()
    {
      pDead = pPoisoned;
    }
};


class Solution
{
  private:
    int pNumberPigs;
    int pNumberBuckets;
    int pTotalSteps;
    
    vector<int> deathPatern;
    
    int ipow(int base, int exp)
    {
        int result = 1;
        while (exp)
        {
            if (exp & 1)
                result *= base;
            exp >>= 1;
            base *= base;
        }
        return result;
    }
  
  public:
    Solution(int numberPigs, int totalSteps ,int numberBuckets)
    {
      pNumberPigs = numberPigs;
      pTotalSteps = totalSteps;
      pNumberBuckets = numberBuckets;
      
      //DeathPatern acts silalarly to the boards
      //the index represents the pig and the value represents the the step it deid on
      // the last value is reserved for if the pigs never deid
      for(int i = 0; i < numberPigs; ++i)
        deathPatern.push_back(totalSteps);
    }
    
    void Step(int step, vector<Pig> pigs)
    {
      for(int i = 0; i < pNumberPigs; ++i)
      {
        if(pigs[i].Dead() && deathPatern[i] == pTotalSteps)
        {
          deathPatern[i] = step;
        }
      }
      
      
      for(int i = 0; i < pNumberBuckets; ++i)
      {
        for(int j = 0; j < pNumberPigs; ++j)
        {
          if(step == (i  % ipow(pTotalSteps + 1, j + 1))/(ipow(pTotalSteps + 1, j)))
            pigs[j].Drink(i);
        }
      }
    }
    
    int Answer()
    {
      //converting the base five in death pattern back into base ten
      int output = 0;
      for(int i = 0; i < pNumberPigs; ++i)
        output += ipow(pTotalSteps + 1, i);
      
      return output;
    }
};


int main() 
{
  int numberPigs;
  cout << "number of pigs: ";
  cin >> numberPigs;
  
  int totalSteps;
  cout << "number of steps: ";
  cin >> totalSteps;
  
  int buckets;
  cout << "number of buckets: ";
  cin >> buckets;
  
  vector<Pig> pigs;
  
  Solution* yourSulution = new Solution(numberPigs, totalSteps, buckets);
  
  srand (time(NULL));
  
  int poisonBucket = rand() % buckets;
  
  cout << "shhh the poison is in bucket number " << poisonBucket;
  
  for(int i = 0; i < numberPigs; ++i)
  {
    pigs.push_back(Pig(poisonBucket));
  }
  
  for(int i = 0; i < totalSteps; ++i)
  {
    yourSulution->Step(i, pigs);
    for(int j = 0; j < numberPigs; ++j)
      pigs[j].Step();
      
  }
  
  cout << "your algorithem says its in bucket" << output;
  
  delete yourSulution;
}