-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC6.cpp
98 lines (86 loc) · 1.36 KB
/
AoC6.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
#include "Lib.h"
#include <iostream>
using std::cout;
int findGreatestBank(vector<int> &a)
{
bool found = false;
int ind = 0;
for (int i = 0; i!=a.size() ; i++)
{
if (!found)
{
found = true;
ind = i;
}
else if (a[i] > a[ind])
{
ind = i;
}
}
return ind;
}
vector<int> redistribute(vector<int> &a, unsigned int col)
{
vector<int> n = a;
int colToRedist = a[col];
n[col] = 0;
col++;
while (colToRedist > 0)
{
if (col == n.size())
{
col %= n.size();
}
n[col]++;
colToRedist--;
col++;
}
return n;
}
bool seenBefore(vector<vector<int>> &dat, vector<int> &needle)
{
for (unsigned int i = 0; i != dat.size(); i++)
{
if (dat[i] == needle)
{
return true;
}
}
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> data = { 2, 8, 8, 5, 4, 2, 3, 1, 5, 5, 1, 2, 15, 13, 5, 14 };
vector<int> db = data;
vector<vector<int>> prevVersions;
bool p2 = false;
int noCycles = 0;
while (true)
{
int gb = findGreatestBank(db);
db = redistribute(db, gb);
if (seenBefore(prevVersions, db))
{
noCycles++;
if (!p2)
{
cout << "part 1 " <<noCycles << "\n";
p2 = true;
noCycles = 0;
prevVersions.clear();
prevVersions.push_back(db);
}
else
{
cout << "part 2 " << noCycles << "\n";
break;
}
}
else
{
prevVersions.push_back(db);
noCycles++;
}
}
return 0;
}